UnexpectedTagNameException:消息:“选择"仅适用于< select>元素,而不是在< li>上使用Selenium从Dropdown中选择li元素时出错 [英] UnexpectedTagNameException: Message: Select only works on <select> elements, not on <li>error selecting li element from a Dropdown using Selenium

查看:214
本文介绍了UnexpectedTagNameException:消息:“选择"仅适用于< select>元素,而不是在< li>上使用Selenium从Dropdown中选择li元素时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望单击新建测试".HTML代码看起来像这样.我是新来的,开始学习使用selenium-python的自动化.

I wish to click on New Test. The HTML code looks something like this. I'm new here and beginning to learn automation using 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>

我要使用的代码

element=driver.find_element_by_id('testing')
drp=Select(element)
drp.select_by_visible_text('New Test')

但是出现错误

selenium.common.exceptions.UnexpectedTagNameException: Message: Select only works on <select> elements, not on <li> 

任何帮助将不胜感激.谢谢!

Any help would be highly appreciated. Thanks!

推荐答案

Select方法仅适用于具有HTML标签选择的下拉菜单.在您的情况下,您不能使用select方法,只需为要从下拉菜单中检测到的元素编写定位符(XPath,CSS或其他).

The Select method works only for dropdowns which has HTML tag select. 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.

在您的情况下,所需元素的XPath应该是:

In your case the XPath of the element which you wanted should be:

//li[text()='New Test']

这篇关于UnexpectedTagNameException:消息:“选择"仅适用于&lt; select&gt;元素,而不是在&lt; li&gt;上使用Selenium从Dropdown中选择li元素时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆