• python-web自动化:下拉列表操作


    select/option元素:

    1.触发下拉列表出现

    2.等待下拉列表中的元素出现,然后进行选择元素即可。

    select/option元素:

    下拉框操作-Select类
    selenium提供Select类来处理select/option

    1.引入

    from selenium.webdriver.support.ui import Select

    2.创建Select对象,传入元素

    ele = driver.find_element_by_xpath(元素定位表达式)
    s = Select(ele)

    3.选择下拉列表值:

    s.select_by_value(value值) #通过value
    s.select_by_index(index) #通过下标
    s.select_by_visible_text(文本') #通过文本

    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains as AC
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    import time
    from selenium.webdriver.support.ui import Select
    
    driver = webdriver.Chrome()
    driver.get('https://www.baidu.com/')
    time.sleep(3)
    # 百度首页,设置链接元素定位
    ele = driver.find_element(By.XPATH,"//div[@id='u1']/a[text()='设置']")
    # 对设置链接进行鼠标悬浮操作
    AC(driver).move_to_element(ele).perform()
    
    # 设置下拉框中选择【高级搜索】,浮窗下拉列表可用click进行点击操作
    WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//a[text()='高级搜索']")))
    driver.find_element_by_xpath("//a[text()='高级搜索']").click()
    
    
    # 等待高级设置搜索页面可见
    WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//select[@name="ft"]')))
    # 创建Select对象,参数为元素
    ele = driver.find_element_by_xpath('//select[@name="ft"]')
    s = Select(ele)
    s.select_by_value('xls') #通过value
    time.sleep(2)
    s.select_by_index(1) #通过下标
    time.sleep(2)
    s.select_by_visible_text('RTF 文件 (.rtf)') #通过文本

    补充!!

    如何定位悬浮下拉列表

    1. 以百度首页为例子,在右上角有设置按钮,鼠标放在设置按钮上,会悬浮显示下拉列表
    2. selenium定位悬浮列表的内容时,需要先定位到设置按钮,然后再按 shift+ctrl+c 定位下拉列表
  • 相关阅读:
    【实用】网站常用单词的中英文对照表
    [译][转]jQuery 1.4 发布:15个新特性实例精讲
    popupWindow 简单实现
    程序员修炼之道 读书笔记
    Burp Suite详细使用教程Intruder模块详解
    漏洞挖掘练习环境(linux方向)
    linux系统操作常见问题(ubuntu和opensuse)
    驱动的加载顺序
    磁盘驱动相关知识
    VS 驱动工程创建软件(EeasySyS)
  • 原文地址:https://www.cnblogs.com/Aphrodite/p/10524459.html
Copyright © 2020-2023  润新知