Selenium 使用匹配项和正则表达式的Xpath在Xpath tester中工作,但在代码抛出InvalidSelectorException中无法执行';评估';在';文件';
HTMLSelenium 使用匹配项和正则表达式的Xpath在Xpath tester中工作,但在代码抛出InvalidSelectorException中无法执行';评估';在';文件';,selenium,selenium-webdriver,xpath,Selenium,Selenium Webdriver,Xpath,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="At
<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>
Xpath表达式
//b[matches(text(),'[0-9]{10}, [A-Za-z]*')]
例外情况
//b[matches(text(),'[0-9]{10}, [A-Za-z]*')]
org.openqa.selenium.InvalidSelectorException:无效选择器:无法
使用xpath表达式查找元素的步骤
//b[匹配(text(),'[0-9]{10}[A-Za-z]'])],因为以下原因
错误:SyntaxError:未能对“文档”执行“评估”:文档
字符串“//b[matches(text(),”[0-9]{10}[A-Za-z]')]”不是有效的
XPath表达式。(会话信息:chrome=70.0.3538.102)(驱动程序
信息:chromedriver=2.42.591088
(7B2B2DCA23CA0862F674758C9A393E685C27D5),平台=Windows NT
10.0.16299 x86_64)(警告:服务器未提供任何stacktrace信息)命令持续时间或超时:0毫秒
Selenium支持XPath 1.0版本,而
fn:matches
来自XPath 2.0
如果要匹配逗号前以10位数字开头的粗体文本,可以在XPath下面进行尝试
//b[string-length(substring-before(., ','))=10 and number(substring-before(., ','))]
如果您对所需元素有更多的约束,请告诉我。我不太清楚您想要xpath搜索正则表达式的确切用例 但是,生成的文本似乎是欢迎信息的一部分,我觉得您的用例可能是提取动态的文本,例如8828024404、MCGM 在这种情况下,您可以使用以下任一解决方案:
- XPath 1:
//div[@class='top-section']/input[@id='role' and @value='admin']//b
- XPath 2:
//div[@class='top-section']/input[@id='role' and @value='admin']/small[contains(.,'Welcome')]//following::b[1]