selenium_webdriver(python)下拉框操作

zx.html

<html>
<body>
<select id="ShippingMethod"  name="ShippingMethod">
<option value="12.51">UPS Next Day Air ==> $12.51</option>
<option value="11.61">UPS Next Day Air Saver ==> $11.61</option>
<option value="10.69">UPS 3 Day Select ==> $10.69</option>
<option value="9.03">UPS 2nd Day Air ==> $9.03</option>
<option value="8.34">UPS Ground ==> $8.34</option>
<option value="9.25">USPS Priority Mail Insured ==> $9.25</option>
<option value="7.45">USPS Priority Mail ==> $7.45</option>
<option value="3.20">USPS First Class ==> $3.20</option>
</select>
</body>
</html>
selenium_webdriver(python)下拉框操作_第1张图片


python:

#coding=utf-8
from selenium import webdriver
import os,time
driver= webdriver.Firefox()

file_path = os.path.abspath('C:\Users\Administrator\Desktop\zx.html')
print file_path
driver.get(file_path)
time.sleep(2)
#先定位到下拉框
m=driver.find_element_by_xpath("//*[@id='ShippingMethod']")
#再点击下拉框下的选项
m.find_element_by_xpath("//*[@id='ShippingMethod']/option[3]").click()
time.sleep(3)
driver.quit()

注意:
这里可能与之前的操作有所不同,首先要定位到下拉框的元素,然后选择下拉列表中的选项进行点击操作。
m=driver.find_element_by_id("ShippingMethod")
m.find_element_by_xpath("//option[@value='10.69']").click()


你可能感兴趣的:(selenium_webdriver(python)下拉框操作)