[英]UnexpectedTagNameException: Message: Select only works on <select> elements, not on <li>error selecting li element from a Dropdown using Selenium
I wish to click on New Test.我希望单击“新建测试”。 The HTML code looks something like this.
HTML 代码看起来像这样。 I'm new here and beginning to learn automation using selenium-python.
我是新来的,开始使用 selenium-python 学习自动化。
<li id="testing">
<ul class="dd">
<li><a href="javascript:toolsPopup('/abc/xyz/text.html');"><span>New Test</span></a></li>
<li><a href="javascript:toolsPopup('/abc/xyz/list.html');"><span>Test List</span></a></li>
</ul>
</li>
The code that I'm trying to use我正在尝试使用的代码
element=driver.find_element_by_id('testing')
drp=Select(element)
drp.select_by_visible_text('New Test')
But getting the error但得到错误
selenium.common.exceptions.UnexpectedTagNameException: Message: Select only works on <select> elements, not on <li>
Any help would be highly appreciated.任何帮助将不胜感激。 Thanks!
谢谢!
The Select method works only for dropdowns which has HTML tag select. Select 方法仅适用于具有 HTML 标记选择的下拉列表。 In your case you can not use a select method, just write locator( XPath, CSS, or else) for the element which you want to detect from the dropdown.
在您的情况下,您不能使用 select 方法,只需为要从下拉列表中检测的元素编写定位器(XPath、CSS 或其他)。
In your case the XPath of the element which you wanted should be:在您的情况下,您想要的元素的 XPath 应该是:
//li[text()='New Test']
As dropdown element with text as New Test is not with in a Select
node you can't use Select
class.由于带有文本为New Test 的下拉元素不在
Select
节点中,因此您不能使用Select
类。 To select <option>
with text as New Test you need to induce WebDriverWait for the element_to_be_clickable()
and you can use the following xpath based Locator Strategies :要选择带有文本的
<option>
作为新测试,您需要为element_to_be_clickable()
引入WebDriverWait ,您可以使用以下基于xpath的定位器策略:
Using CSS_SELECTOR
:使用
CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li#testing > ul.dd li > a[href*='/abc/xyz/text.html'] > span"))).click()
Using XPATH
:使用
XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@id='testing']/ul[@class='dd']//li/a/span[text()='New Test']"))).click()
Note : You have to add the following imports:注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
You can find a couple of relevant discussions in:您可以在以下位置找到一些相关讨论:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.