【问题标题】:Select Element in Python Selenium for Dropdown Menu在 Python Selenium 中为下拉菜单选择元素
【发布时间】:2019-05-07 04:16:22
【问题描述】:

我是 Python 新手,我正在尝试使用 Selenium 从 Firefox 浏览器的下拉菜单中选择一个值。

这就是我正在尝试的。请让我知道我做错了什么:

from selenium import webdriver

from selenium.webdriver.support.ui import Select

browser = webdriver.Firefox()

browser.get('URL')

select = Select(browser.find_element_by_id('Yesterday'))

我需要在我的原始帖子中添加以下内容:

我使用 Selenium IDE 来尝试识别选择器。请下图。我想从下拉列表中选择“昨天”。

enter image description here

【问题讨论】:

  • Select的定位器是什么
  • 请检查我的回答,
  • 页面中有多少个下拉菜单?

标签: python selenium drop-down-menu


【解决方案1】:

你可以这样使用,找到Select下拉然后选择自定义值

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_name('locator of the select'))
select.select_by_value('Custom')

另外,你可以使用索引

select = Select(driver.find_element_by_name('locator of the select'))
select.select_by_index(6)

【讨论】:

  • 感谢您的建议。我已经编辑了我的帖子并通过 Selenium IDE 添加了屏幕截图。我仍在尝试弄清楚如何在 Python 中编写代码。
【解决方案2】:

您可以使用 browser.find_element_by_link_text("Yesterday") 代替 browser.find_element_by_id。由于它不包含 id 标签,因此无法使用。

【讨论】:

  • 感谢您的建议。我已经编辑了我的帖子并通过 Selenium IDE 添加了屏幕截图。我仍在尝试弄清楚如何在 Python 中编写代码。
猜你喜欢
  • 2021-03-13
  • 1970-01-01
  • 2023-03-25
  • 2022-11-11
  • 2021-04-28
  • 2018-02-17
  • 2016-04-13
  • 2019-02-25
  • 1970-01-01
相关资源
最近更新 更多