gpt4 book ai didi

java - 在 Selenium xpath 定位器中转义美元符号

转载 作者:行者123 更新时间:2023-11-30 07:05:09 25 4
gpt4 key购买 nike

我想定位一个属性为 $9a 的元素,但显然美元符号导致了问题。当我使用表达式时:

//td[@id='isc_6T']//span[@$9a='browse'] 

我得到以下异常

The given selector //td[@id='isc_6T']//span[@$9a='browse'] is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression //td[@id='isc_6T']//span[@$9a='browse'] because of the following error:
SyntaxError: The expression is not a legal expression.
Command duration or timeout: 9 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.43.1', revision: '5163bceef1bc36d43f3dc0b83c88998168a363a0', time: '2014-09-10 09:43:55'
System info: host: 'daniel', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-39-generic', java.version: '1.7.0_65'
Session ID: 7fdd584b-10b8-4c5a-ab64-72fe7cff7e2a
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=LINUX, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=33.0}]

编辑:这是我要查找的 HTML 部分。 (id是动态的所以不能直接使用)

<span handlenativeevents="false" $9a="browse" id="isc_T9" role="button">
<img border="0" align="TEXTTOP" height="18" width="18" draggable="true" suppress="TRUE" style="vertical-align:middle;margin-top:0px;margin-bottom:0px;" id="isc_TA" src="some_image.png">
</span>

我已经创建了一个使用 Jsoup 解析 HTML 以确定当前动态 ID 的变通方法,但我仍然想要一个更简洁的解决方案

最佳答案

“$”符号不能出现在 XML 属性名称中,名称的 XPath 语法反射(reflect)了 XML 语法。

根据这个答案:

What characters are allowed in an HTML attribute name?

HTML5 属性名称中允许使用美元符号和许多其他内容。但是,除非有人定义了从这些名称到有效 XML 名称的映射,否则不能从 XPath 中引用它们;而且我不知道有任何此类映射。

关于java - 在 Selenium xpath 定位器中转义美元符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27060726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com