【问题标题】:Selenium Python not able to select value from drop down but i can click it to open the drop downSelenium Python 无法从下拉列表中选择值,但我可以单击它打开下拉列表
【发布时间】:2016-04-07 10:00:13
【问题描述】:

我有一个奇怪的问题。我有一个下拉元素,我想选择值“否”。我的 Selenium Python 代码不会选择值“No”。 我尝试单击该元素以查看单击是否有效以及该元素是否可以交互、可见等。
点击有效,下拉元素打开。

我的 Selenium Python 代码是:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select

def select_use_for_matching_dropdown(self, value):
    # Params value: The value for the Matching drop down Yes or No
    try:
        select = Select(WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, 'data_configuration_edit_data_object_tab_details_lb_use_for_match'))))
        select.select_by_visible_text(str("No"))
    except NoSuchElementException, e:
        print "Element not found "
        print e
        self.save_screenshot("select_use_for_matching_dropdown")

HTML 是:

 <select id="data_configuration_edit_data_object_tab_details_lb_use_for_match" class="gwt-ListBox marginright">
    <option value="yes">yes</option>
    <option value="no">no</option>
    <option value="exclude data categories">exclude data categories</option>
</select>

有没有其他方法可以尝试选择值“否”

我也试过

select = Select(self.driver.find_element_by_id('data_configuration_edit_data_object_tab_details_lb_use_for_match'))

select.select_by_visible_text('No')

谢谢,里亚兹

【问题讨论】:

  • 看看它是否对你有帮助 stackoverflow.com/questions/36471904/… 如果你想要 java 等价物,那么我可以提供一个示例代码
  • 感谢链接,已经试过了。我的其他下拉元素有效。这个有点奇怪,它没有选择值。
  • 我认为您要选择的元素已加载到 DOM 中(EC 工作),但是当您在该时间点选择选项 No 时,选项 No 的位置在 DOM 内部不固定,因此一种可能解决方案是在使用选项 no 直接进入 dd 之前,请执行一些其他任务,它将不允许任何选项在 DOM 中获取其位置,之后它将像魅力一样工作
  • 是的,我的一些其他元素不得不这样做。感谢您的提示
  • 我很高兴这个提示对您有所帮助

标签: python-2.7 selenium selenium-webdriver


【解决方案1】:

试试下面的任何一个:-

select.select_by_visible_text('no')

select.select_by_value('no')

希望对你有帮助:)

【讨论】:

    【解决方案2】:

    不确定在 Python 驱动程序中大写是否重要,但您的实际值是小写“否”,因此您可能想尝试一下

    select.select_by_visible_text('no')
    

    【讨论】:

      【解决方案3】:

      希望这段代码对你有所帮助。

      from selenium.webdriver.support.ui import Select
      select= Select(driver.find_element_by_id('id_of_element'))
      

      从给定的下拉菜单中选择“否”选项

      1. 使用 index 属性选择选项的 select_by_index() 方法。

      select.select_by_index(1)

      1. 使用 value 属性选择选项的 select_by_value() 方法。

      select.select_by_value('no')

      1. 您可以使用匹配下拉菜单中显示的文本。

      select.select_by_visible_text('no')

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-13
        • 1970-01-01
        • 2022-10-13
        • 1970-01-01
        • 1970-01-01
        • 2016-04-14
        • 2015-05-19
        • 1970-01-01
        相关资源
        最近更新 更多