【问题标题】:NoSuchElementException: Message: no such element: Unable to locate element while trying to select options within a drop down boxNoSuchElementException:消息:没有这样的元素:尝试在下拉框中选择选项时无法找到元素
【发布时间】:2019-01-14 08:51:55
【问题描述】:

我正在尝试创建一个脚本,该脚本可以自动单击下拉框并单击我想要的选项。我已经尝试实现另一个类似问题的代码,但是我收到了一条错误消息。

使用类似问题的解决方案,我尝试了这行代码:

driver.find_element_by_xpath("//select[@name='interface']/option[text()='Management']").click()

HTML

<select class="col-1 custom-select" name="interface" id="interface" required="required">
  <option selected="" disabled="" class="hideoption">Select Interface</option>
  <option value="InterfaceLAN">Production</option>
  <option value="MgmtLAN">Management</option>
  <option value="Clustering">Clustering</option>
</select>

我想自动执行单击下拉框并选择“管理”选项的过程。但是,我收到一条错误消息,如图所示:

NoSuchElementException: Message: no such element: Unable to locate element:"method":"xpath","selector":"//select[@name='interface']/option[text()='Management']"}

【问题讨论】:

  • @Andersson 现在你决定删除你对这个问题的回答,强行关闭这个讨论作为一个重复是完全不道德的。这是一个明显滥用提供给您的特权,因为重复是一个金牌 i> 带有selenium 标签的支架。
  • @DebanjanB 发布重复的答案不是完全不道德吗?

标签: python selenium selenium-webdriver xpath webdriverwait


【解决方案1】:

由于所需的元素是 select 元素,您需要使用 Select 类并诱导 WebDriverwait 以使所需的元素可见,您可以使用以下解决方案:

from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# other lines of code
select = Select(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//select[@id='interface' and @name='interface']"))))
select.select_by_value("MgmtLAN")

【讨论】:

    【解决方案2】:

    尝试使用 Select Class ,在此处提供下拉菜单的 xpath。 然后尝试通过值、索引或可见文本选择下拉列表

    代码:

    from selenium.webdriver.support.ui import Select
    
    select = Select(driver.find_element_by_name('name'))
    select.select_by_index(index)
    select.select_by_visible_text("text")
    select.select_by_value(value)
    

    【讨论】:

      猜你喜欢
      • 2019-06-08
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多