【问题标题】:How to select a value from a drop-down using Selenium and Python如何使用 Selenium 和 Python 从下拉列表中选择一个值
【发布时间】:2017-04-21 11:07:23
【问题描述】:

我想使用 Selenium 和 Python 从选择字段中选择一个选项。

HTML如下:

<select autocomplete="off" class="style_input_item" name="AccountEnable" id="Enable" value="0" onchange="onPageDataChange()">
    <option value="0" selected="selected"><script>T("Disabled")</script>Disabled</option>
    <option value="1"><script>T("Enabled")</script>Enabled</option>
</select>

我尝试如下:

driver.find_element_by_xpath('//*[@id="Enable"]/option[value="1"]').click()

我收到了那个错误:

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//*[@id="Enable"]/option[值="0"]"}

【问题讨论】:

标签: python-3.x selenium select xpath


【解决方案1】:

试试吧:

mydriver.find_element_by_xpath('//*[@id="Enable"]/option[@value="1"]').click()

mydriver.find_element_by_xpath('//*[@id="Enable"]/option[2]').click()

【讨论】:

    【解决方案2】:

    确保包含 Select:

    from selenium.webdriver.support.select import Select
    

    然后

    select = Select(driver.find_element_by_id('Enable'))
    select.select_by_index(0)
    

    【讨论】:

      猜你喜欢
      • 2016-11-09
      • 1970-01-01
      • 2020-05-20
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2020-08-02
      相关资源
      最近更新 更多