select下拉框
一.select下拉框
场景:
百度首页->设置->搜索设置
定位方法:
1.直接使用xpath进行定位
driver.find_element_by_xpath(".//*[@id='nr']/option[2]").click()
代码:
#coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(2)
#鼠标悬停,定位百度"设置"
mouse=driver.find_element("link text","设置")
ActionChains(driver).move_to_element(mouse).perform()
time.sleep(2)
driver.find_element("link text","搜索设置").click()
time.sleep(2)
#使用xpath直接定位select下拉框
driver.find_element_by_xpath(".//*[@id='nr']/option[3]").click()
time.sleep(2)
driver.quit()
2.先定位下拉框,再定位选项
s=driver.find_element_by_id("nr")
s.find_element_by_xpath("//option[@value='50']").click()
代码:
#coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(2)
#鼠标悬停,定位百度"设置"
mouse=driver.find_element("link text","设置")
ActionChains(driver).move_to_element(mouse).perform()
time.sleep(2)
driver.find_element("link text","搜索设置").click()
time.sleep(2)
#先定位下拉框再定位选项
parent=driver.find_element_by_id("nr")
parent.find_element_by_xpath("//option[@value='50']").click()
time.sleep(2)
driver.quit()
2.1直接在使用一行代码代替方法2的两行
driver.find_element_by_id("nr").find_element_by_xpath("//option[@value=''50]").click()
代码:
#coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(2)
#直接在使用一行代码代替方法2的两行
mouse=driver.find_element("link text","设置")
ActionChains(driver).move_to_element(mouse).perform()
time.sleep(2)
driver.find_element("link text","搜索设置").click()
time.sleep(2)
driver.find_element_by_id("nr").find_element_by_xpath("//option[@value='50']").click()
time.sleep(2)
driver.quit()
3.导入select类定位(from selenium.webdriver.support.select import Select)
*使用以下操作不需要点击下拉框,而是直接选中,这样更高效,推荐
(1)select_by_value
代码:
#coding:utf-8
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
import time
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(2)
#直接在使用一行代码代替方法2的两行
mouse=driver.find_element("link text","设置")
ActionChains(driver).move_to_element(mouse).perform()
time.sleep(2)
driver.find_element("link text","搜索设置").click()
time.sleep(2)
#定位id为"nr"的select中value值为30的选项
s=driver.find_element_by_id("nr")
Select(s).select_by_value("20")
time.sleep(2)
driver.quit()
(2)select_by_index(0)
代码:
#coding:utf-8
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
import time
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(2)
#直接在使用一行代码代替方法2的两行
mouse=driver.find_element("link text","设置")
ActionChains(driver).move_to_element(mouse).perform()
time.sleep(2)
driver.find_element("link text","搜索设置").click()
time.sleep(2)
#定位id为"nr"的select中的下标来选择
s=driver.find_element_by_id("nr")
Select(s).select_by_index(2)
time.sleep(2)
driver.quit()
(3)select_by_visibel_text("每页显示20条")
代码:
#coding:utf-8
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
import time
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(2)
#直接在使用一行代码代替方法2的两行
mouse=driver.find_element("link text","设置")
ActionChains(driver).move_to_element(mouse).perform()
time.sleep(2)
driver.find_element("link text","搜索设置").click()
time.sleep(2)
#根据下拉框的text文本直接定位
s=driver.find_element_by_id("nr")
Select(s).select_by_visible_text("每页显示20条")
time.sleep(2)
driver.quit()
ps:
获取所有选项的元素对象(all_selected_options)
代码:
#coding:utf-8
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
import time
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(2)
#直接在使用一行代码代替方法2的两行
mouse=driver.find_element("link text","设置")
ActionChains(driver).move_to_element(mouse).perform()
time.sleep(2)
driver.find_element("link text","搜索设置").click()
time.sleep(2)
#返回所有选项的元素对象
s=driver.find_element_by_id("nr")
all=Select(s).all_selected_options #有括号的叫方法,这种没有括号的叫属性
print all
print len(all)#获取选项的个数
time.sleep(2)
driver.quit()