六 定位select下拉框和非下拉框

Select下拉框

select01.html的代码如下,用这个演示。百度高级搜索那个页面不是select下拉框了`




    
    下拉框







image.png

select下拉框代码演示

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import os
from time import sleep


driver = webdriver.Chrome()

# 将html文件更改为自己的路径
driver.get("file:///"+os.getcwd()+os.sep+"select01.html")
driver.maximize_window()

# 找到select标签元素
pro = Select(driver.find_element_by_id("pro"))

# 返回所有选项
for option in pro.options:
    print(option.text)

# 返回所有被选中的选项
for option in pro.all_selected_options:
    print(option.text)

# 通过value选中
pro.select_by_value("gj")
sleep(1)

# 通过index选中
pro.select_by_index(1)
sleep(1)

# 通过标签文本选中
pro.select_by_visible_text("广东")

取消选中

# 找到id=city的下拉框
city = Select(driver.find_element_by_id("city"))

# 全选
for option in city.options:
    if not option.is_selected():
        city.select_by_visible_text(option.text)
sleep(1)

# 根据value取消选中
city.deselect_by_value("bj")
sleep(1)

# 根据index取消选中
city.deselect_by_index(0)
sleep(1)

# 根据标签文本选中
city.deselect_by_visible_text("武汉")
sleep(1)

# 全选
for option in city.options:
    if not option.is_selected():
        city.select_by_visible_text(option.text)
sleep(1)

# 取消选中所有选项
city.deselect_all()

知识点
取消操作只适用于添加了multiple的下拉框,否则会报错

    raise NotImplementedError("You may only deselect options of a multi-select")
NotImplementedError: You may only deselect options of a multi-select

Select源码解读

class Select(object):

    def __init__(self, webelement):
        """
        Constructor. A check is made that the given element is, indeed, a SELECT tag. If it is not,
        then an UnexpectedTagNameException is thrown.

        :Args:
         - webelement - element SELECT element to wrap

        Example:
            from selenium.webdriver.support.ui import Select \n
            Select(driver.find_element_by_tag_name("select")).select_by_index(2)
        """

知识点
实例化 Select 需要传入 select 下拉框的 webelement
若传入 webelementtag_name 不是 标签,则会抛出异常 UnexpectedTagNameException

定位非下拉框

百度的高级搜索 跳转页面的非下拉框

image.png

  • 非select /option的下拉框,通过点击之后,在定位。
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import time,platform,os
if platform.system()=="Windows":
    os.system("taskkill -im chrome* -f")
else:
    os.system("killall -9 chrome*")

driver=webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.baidu.com")
time.sleep(5)
#鼠标悬停在‘设置’按钮
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH, '//div[@id="u1"]//span[text()="设置" and @id="s-usersetting-top"]')))
s=driver.find_element(By.XPATH,"""//div[@id="u1"]//span[text()="设置" and @id="s-usersetting-top"]""")
time.sleep(3)
ActionChains(driver).move_to_element(s).perform()
driver.find_element_by_link_text("高级搜索").click()
time.sleep(3)
#定位下拉框,再点击选项

driver.find_element_by_css_selector("#adv-setting-gpc span.c-select-selected-value").click()
# 非select option 用 Select类去处理会报错
# ele = driver.find_element_by_css_selector("#adv-setting-gpc span.c-select-selected-value")
# Select(ele).select_by_visible_text("最近一月")

time.sleep(3)
driver.find_element(By.XPATH, "//p[contains(text(),'最近一月')]").click()

time.sleep(3)
driver.quit()

你可能感兴趣的:(六 定位select下拉框和非下拉框)