NoSuchElementException:消息:没有这样的元素:尝试在下拉框中选择 select 选项时无法定位元素
NoSuchElementException: Message: no such element: Unable to locate element while trying to select options within a drop down box
我正在尝试创建一个脚本,它可以自动点击下拉框并点击我想要的选项。我已经尝试实现另一个类似问题的代码,但是我收到一条错误消息。
使用类似问题的解决方案,我尝试了这行代码:
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>
我想自动执行单击下拉框并选择选项 'Management' 的过程。但是,我收到了一条错误消息,如下所示:
NoSuchElementException: Message: no such element: Unable to locate element:"method":"xpath","selector":"//select[@name='interface']/option[text()='Management']"}
由于所需的元素是 select
元素,您需要使用 Select
class 并为所需的 [=] 引入 WebDriverwait 15=]元素可见 可以使用以下解决方案:
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")
尝试使用 Select Class ,在那里提供下拉菜单的 xpath。
然后尝试通过值、索引或可见文本 select 下拉列表
代码:
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)
我正在尝试创建一个脚本,它可以自动点击下拉框并点击我想要的选项。我已经尝试实现另一个类似问题的代码,但是我收到一条错误消息。
使用类似问题的解决方案,我尝试了这行代码:
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>
我想自动执行单击下拉框并选择选项 'Management' 的过程。但是,我收到了一条错误消息,如下所示:
NoSuchElementException: Message: no such element: Unable to locate element:"method":"xpath","selector":"//select[@name='interface']/option[text()='Management']"}
由于所需的元素是 select
元素,您需要使用 Select
class 并为所需的 [=] 引入 WebDriverwait 15=]元素可见 可以使用以下解决方案:
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")
尝试使用 Select Class ,在那里提供下拉菜单的 xpath。 然后尝试通过值、索引或可见文本 select 下拉列表
代码:
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)