gpt4 book ai didi

selenium - Xpath 使用匹配项和正则表达式在 Xpath 测试器中工作,但在代码中抛出 InvalidSelectorException 无法在 'evaluate' 上执行 'Document'

转载 作者:行者123 更新时间:2023-12-02 21:29:40 33 4
gpt4 key购买 nike

HTML

<div class="top-section" style="" xpath="1">
<input id="role" value="admin" hidden="">
<small>Welcome </small> <b style="">8828024404, MCGM</b>
<a href=""><img alt="Attendance" width="" height="" src="css/assets/images/logo.png"></img></a>
<a href="/logout" class="float-right logout">Log Out</a>
</input>
</div>

要搜索的字符串

8828024404, MCGM

Xpath 表达式

//b[matches(text(),'[0-9]{10}, [A-Za-z]*')]

异常

org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //b[matches(text(),'[0-9]{10}, [A-Za-z]')] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//b[matches(text(),'[0-9]{10}, [A-Za-z]')]' is not a valid XPath expression. (Session info: chrome=70.0.3538.102) (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.16299 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds

最佳答案

Selenium 支持 XPath 1.0 版本,而 fn:matches 来自 XPath 2.0。

如果您想匹配以逗号前 10 位数字开头的粗体文本,您可以尝试下面的 XPath

//b[string-length(substring-before(., ','))=10 and number(substring-before(., ','))]

如果您对所需元素有更多限制,请告诉我

关于selenium - Xpath 使用匹配项和正则表达式在 Xpath 测试器中工作,但在代码中抛出 InvalidSelectorException 无法在 'evaluate' 上执行 'Document',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53354813/

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