无法通过使用 XPATH 查询属性(包含连字符)值来获取元素 [英] Unable to get an element via query on attribute(containing hyphens) value using an XPATH

查看:36
本文介绍了无法通过使用 XPATH 查询属性(包含连字符)值来获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 XPATH 获取具有属性的元素时遇到了一个非常奇怪的场景.问题是,如果属性中包含多个连字符,则 Selenium 无法找到该元素.我正在使用 PythonSelenium 2.52.0

I'm facing a very weird scenario in getting an element having an attribute using an XPATH. The problem is that if the attribute contains more than one Hyphens in it then Selenium is unable to find that element. I am using Python and Selenium 2.52.0

工作正常的示例.

<span data-one="containingOnlyOneHyphen"> </span>
XPATH = '//*[@data-one="containingOnlyOneHyphen"]' (Works Completely Fine)

不起作用的例子.

<span data-one-two="containingTwoHyphen"> </span>
XPATH = '//*[@data-one-two="containingTwoHyphen"]' (Does not work :/ )

到目前为止,我对这个问题的理解是 Selenium 无法根据包含超过 1 个连字符的属性找到元素.

So far my understanding with this issue is that Selenium is unable to find element based on attribute having more than 1 hyphens in it.

如果您能指导我有关此问题的任何信息,那就太好了.我几乎什么都试过了,但都失败了.

It would be great if you can guide me anything regarding this issue. I've almost tried everything but failed.

谢谢&亲切的问候,

Thanks & kind Regards,

哈桑·马哈茂德

推荐答案

您是否尝试过为此使用 cssSelector ?

Have you tried using cssSelector for this ?

span[data-one-two=containingTwoHyphen]

据我所知,效果很好.通过 jsfiddle.net 和 Chrome 开发人员选项搜索进行测试.

works well, according to me. Tested via jsfiddle.net and chrome developer options search.

这篇关于无法通过使用 XPATH 查询属性(包含连字符)值来获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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