简体   繁体   English

UnexpectedTagNameException:消息:选择仅适用于<select>元素,不在<li>使用 Selenium 从下拉列表中选择 li 元素时出错

[英]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 based Locator Strategies :要选择带有文本的<option>作为新测试,您需要为element_to_be_clickable()引入WebDriverWait ,您可以使用以下基于定位器策略

  • 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

Reference参考

You can find a couple of relevant discussions in:您可以在以下位置找到一些相关讨论:

如何从不在下拉列表中的 select 值<div>使用 selenium python</div><div id="text_translate"><p> 我有下面的 html 代码,我想点击下拉列表和 select 第一个值。如何实现这一点。我在从下拉列表中选择值时遇到问题,但能够单击下拉列表</p><pre>&lt;div id= "location-select-list" class="mb-list" role="role0"&gt; &lt;mb-option id='1' class='classname' role='rolename' data-auto-id="dt1" aria-disabled="False" 1 &lt;/mb-option&gt; &lt;mb-option id='2' class='classname' role='rolename' data-auto-id="dt2"aria-disabled="False" 2 &lt;/mb-option&gt;</pre><p> 我已经尝试过了,但没有工作。</p><pre> #click on the dropdown--working WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, xpath_0))).click() #selecting 1st value from the dropdown value list--not working xpath = "//div[@id='location-select-list']//mb-option[@data-auto-id='dt1']" WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, xpath))).click()</pre></div> - How to select value from dropdown which is not in <div> using selenium python

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用xpath从ul中选择li元素 - Select li elements from ul with xpath 无法使用 select 选择下拉选项:UnexpectedTagNameException selenium:python - Not able to select drop-down option using select : UnexpectedTagNameException selenium : python 使用Python + Selenium选择一个下拉菜单 - Select a dropdown using Python + Selenium 如何使用Python Selenium从Bootstrap下拉列表中选择元素? - How do I select an element from a Bootstrap dropdown using Python Selenium? 使用Selenium来查找数量变化的动态li元素 - Locating a Dynamic li element with changing number using Selenium 如何从不在下拉列表中的 select 值<div>使用 selenium python</div><div id="text_translate"><p> 我有下面的 html 代码,我想点击下拉列表和 select 第一个值。如何实现这一点。我在从下拉列表中选择值时遇到问题,但能够单击下拉列表</p><pre>&lt;div id= "location-select-list" class="mb-list" role="role0"&gt; &lt;mb-option id='1' class='classname' role='rolename' data-auto-id="dt1" aria-disabled="False" 1 &lt;/mb-option&gt; &lt;mb-option id='2' class='classname' role='rolename' data-auto-id="dt2"aria-disabled="False" 2 &lt;/mb-option&gt;</pre><p> 我已经尝试过了,但没有工作。</p><pre> #click on the dropdown--working WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, xpath_0))).click() #selecting 1st value from the dropdown value list--not working xpath = "//div[@id='location-select-list']//mb-option[@data-auto-id='dt1']" WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, xpath))).click()</pre></div> - How to select value from dropdown which is not in <div> using selenium python 如何使用 Selenium 和 Python 从下拉列表中获取 select 的值 - How to select a value from DropDown list using Selenium and Python 如何使用 selenium 从内部 li 中提取文本 - how to extarct text from a inner li using selenium Webdriver Selenium 单击<li> Iframe 内的元素 - Webdriver Selenium click a <li> element inside an Iframe Selenium Wrapper 从下拉列表中随机选择一个元素(提供下拉列表的位置(xpath,css,...))? - Selenium Wrapper to randomly select an element from a dropDown ( the location (xpath, css, ...) of the dropdown is provided)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM