Select 和Alert使用前都必须先导入
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.select import Select
以下拉选择分别为JAVA、PYTHIN、.NET、C++为例说明Select
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.support.select import Select
from time import sleep
driver = webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('file:///C:/Users/Deshang008/Desktop/select.html')
select=Select(driver.find_element_by_id('yuyan'))
#选项的value值
select.select_by_value('python')
#选项的文本
select.select_by_visible_text('.NET')
#选项的索引,下标从0开始
select.select_by_index(u'2')
sleep(3)
driver.quit()
代码如下:
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://www.baidu.com/')
shezhi=driver.find_element_by_link_text(u'设置')
ActionChains(driver).move_to_element(shezhi).perform()
driver.find_element_by_css_selector('.setpref').click()
driver.find_element_by_link_text(u'保存设置').click()
driver.switch_to_alert().accept()
time.sleep(5)
driver.quit()