【发布时间】:2016-11-16 07:53:33
【问题描述】:
我是在 Python 中使用 Selenium 的新手,我正在尝试访问 Barclays Live 网站上的索引数据。登录并加载页面后,我会尝试从页面的下拉列表中选择“Custom1”。与列表关联的 HTML 代码中的选择对象如下所示:
<select name="customViewId" class="formtext" onchange="submitFromSelect('username');return false;">
<option value=""> </option>
<option value="Favorite Indices">Favorite Indices</option>
<option value="Custom1">Custom1</option>
<option value="CB group">CB group</option>
<option value="Kevin Favorites">Kevin Favorites</option>
<option value="LB Gov/Cdt intermediate">LB Gov/Cdt intermediate</option>
</select>
这是我的代码,直到我尝试访问该对象:
from selenium import webdriver
from selenium.webdriver.support.select import Select
#Get chrome driver and connect to Barclays live site
browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe")
browser.get('https://live.barcap.com/')
#Locate username box and enter username
username = browser.find_element_by_name("user")
username.send_keys("username")
#Locate password box and send password
password = browser.find_element_by_name("password")
password.send_keys("password")
#Click login button
login = browser.find_element_by_id("submit")
login.click()
#Open page where you can select indices
browser.get("https://live.barcap.com/BC/barcaplive?menuCode=MENU_IDX_1061")
我已经尝试了许多我发现的建议解决方案,通常会出现错误“无法找到元素:”,然后是我尝试访问选择对象的任何方法。我似乎无法通过名称、xpath 或使用 Select() 函数来访问它。我尝试在代码中加入等待时间,以防元素尚未加载,但没有运气。一些我希望工作的事情的例子,但不是:
select_box = browser.find_element_by_name("customViewId")
select_box = browser.find_element_by_xpath("//select[option[@value='Custom1']]"
我的背景不是编程,如果这是一个愚蠢的问题,请放轻松。在此先感谢您的帮助。
【问题讨论】:
-
您能检查一下这个
select元素是否位于iframe内吗? -
select 元素确实位于 iframe 中。
标签: javascript python html selenium xpath