Selenium-Webdriver(python) (十) --- 下拉框的选择

来源:互联网 发布:centos 7 minimal ssh 编辑:程序博客网 时间:2024/05/17 18:26

HTML源码:

   <select id="WG_BankID" name="bank">
    <option value="">请选择银行</option>
    <option value="23">温州银行</option>
    <option value="20">南京银行</option>
    <option value="19">金华银行</option>
</select>

webdriver脚本:

方法1:driver.find_element_by_id("WG_BankID").find_elements_by_tag_name("option")[2].click()

方法2:

select=driver.find_element_by_xpath("//select[@id=' WG_BankID ']")

select.find_element_by_xpath("//option[@value='20']").click()

方法3:

from selenium.webdriver.support.ui import Select #在脚本开头导入

select = Select(driver.find_element_by_tag_name("select")) #直接套用不需要修改

select.select_by_visible_text("南京银行") #通过显示的文字来选择

 

执行结果:南京银行被选中

 

 

0 0
原创粉丝点击