【问题标题】:Can't access dropdown select using Selenium in Python无法在 Python 中使用 Selenium 访问下拉选择
【发布时间】:2016-11-16 07:53:33
【问题描述】:

我是在 Python 中使用 Selenium 的新手,我正在尝试访问 Barclays Live 网站上的索引数据。登录并加载页面后,我会尝试从页面的下拉列表中选择“Custom1”。与列表关联的 HTML 代码中的选择对象如下所示:

<select name="customViewId" class="formtext" onchange="submitFromSelect('username');return false;">
    <option value="">&nbsp;</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


【解决方案1】:

一种通过向其发送键来更改选择元素的方法。

首先找到选择元素,然后将键发送给它。您通常只需要前 1-3 个字母即可从元素中获得正确(唯一)的结果。

select_elem = browser.find_element_by_name("customViewId")
select_elem.send_keys("cu")

另一种选择是发送箭头键或类似的东西,或者在选择时执行.click(),然后从下拉列表中选择,或者如果在不先点击的情况下发送键不起作用。

另外,你还可以找到相对于另一个元素的元素,例如:

select_box = browser.find_element_by_name("customViewId")
option = select_box.find_element_by_xpath("/[option[@value='Custom1']]") #example only - untested for this case

最后,另一种选择是任意执行一些 JavaScript 以使用 browser.execute_script 更改元素——但编写 JS 代码来做到这一点可能超出了这里的范围。

YMMV 取决于特定的页面/方法。

【讨论】:

  • 不幸的是,这对我不起作用。在尝试首先找到选择元素时会引发错误。 browser.find_element_by_name 会导致错误。
  • 好的,我明白了。您的问题不是选择选项...而是...是选择选择元素开始。我误解了,不知道它在 iFrame 中
【解决方案2】:

select 元素确实位于 iframe 中。

这意味着您应该switch 进入框架的上下文,然后才能找到元素:

browser.switch_to.frame("frame_name_or_id")
select_box = browser.find_element_by_name("customViewId")

如果您需要从框架的上下文中返回,请使用:

browser.switch_to.default_content()

至于操作选择框部分,还有一个更好的方法——使用Select class

from selenium.webdriver.support.select import Select

select_box = Select(browser.find_element_by_name("customViewId"))
select_box.select_by_visible_text("CB group")

【讨论】:

    【解决方案3】:

    我也尝试了类似上述的各种方法来解决这个问题。 不幸的是,我无法使用上述方法解决这个问题。 但是,我的问题已经解决了使用发送点击事件到动作元素。

    select = browser.find_element_by_xpath("//select[@id='reservation_period']")
    for option in select.find_elements_by_xpath(".//option"):
        if option.text == "2 Days":
            option.click()
            break
        select.send_keys(Keys.ARROW_DOWN)
    

    【讨论】:

      猜你喜欢
      • 2017-06-16
      • 2021-04-26
      • 2023-04-01
      • 1970-01-01
      • 2020-11-16
      • 2022-01-12
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      相关资源
      最近更新 更多