首页 > 代码库 > 【selenium自动化——下拉框处理】
【selenium自动化——下拉框处理】
<html>
<body>
<select id="ShippingMethod" onchange="updateShipping(options[selectedIndex]);"
name="ShippingMethod">
<option value="http://www.mamicode.com/12.51">UPS Next Day Air ==> $12.51</option>
<option value="http://www.mamicode.com/11.61">UPS Next Day Air Saver ==> $11.61</option>
<option value="http://www.mamicode.com/10.69">UPS 3 Day Select ==> $10.69</option>
<option value="http://www.mamicode.com/9.03">UPS 2nd Day Air ==> $9.03</option>
<option value="http://www.mamicode.com/8.34">UPS Ground ==> $8.34</option>
<option value="http://www.mamicode.com/9.25">USPS Priority Mail Insured ==> $9.25</option>
<option value="http://www.mamicode.com/7.45">USPS Priority Mail ==> $7.45</option>
<option value="http://www.mamicode.com/3.20" selected="">USPS First Class ==> $3.20</option>
</select>
</body>
</html>
通过脚本选择下拉列表里的$10.69:
#-*-coding=utf-8
from selenium import webdriver
import os,time
driver= webdriver.Firefox()
file_path = ‘file:///‘ + os.path.abspath(‘drop_down.html‘)
driver.get(file_path)
time.sleep(2)
#先定位到下拉框
m=driver.find_element_by_id("ShippingMethod")
#再点击下拉框下的选项
m.find_element_by_xpath("//option[@value=http://www.mamicode.com/‘10.69‘]").click()
time.sleep(3)
driver.quit()
【selenium自动化——下拉框处理】