gpt4 book ai didi

java - Selenium |命名空间错误: Failed to execute 'evaluate' on 'Document' | Contains unresolvable namespaces

转载 作者:行者123 更新时间:2023-12-02 09:53:23 24 4
gpt4 key购买 nike

我可以使用浏览器的开发人员工具检查元素,如下所示:

使用 FireFox 检查元素:

/html/body/div[1]/div/div[8]/form/div[2]/spring:eval/table/tbody/tr[2]/td[5]

使用 Chrome 检查相同的元素,如下所示:

//*[@id="searchVO"]/div[2]/spring:eval/table/tbody/tr[2]/td[5]

但是,当我尝试在脚本中使用任何这些 XPath 时,执行会失败并显示以下堆栈跟踪:

WebElement formElement = wd.findElement(By.xpath("//*[@id=\"searchVO\"]/div[2]/spring:eval/table/tbody/tr[2]/td[5]"));

或者

WebElement formElement = wd.findElement(By.xpath("/html/body/div[1]/div/div[8]/form/div[2]/spring:eval/table/tbody/tr[2]/td[5]"));

堆栈跟踪:

org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //*[@id="searchVO"]/div[2]/spring:eval/table/tbody/tr[2]/td[5] because of the following error:
NamespaceError: Failed to execute 'evaluate' on 'Document': The string '//*[@id="searchVO"]/div[2]/spring:eval/table/tbody/tr[2]/td[5]' contains unresolvable namespaces.



*** Element info: {Using=xpath, value=//*[@id="searchVO"]/div[2]/spring:eval/table/tbody/tr[2]/td[5]}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)

我也确认网页上没有Frames;然而,上面的元素存在于表单中 - 下面给出了该元素的片段 - 我试图找到下面的状态“正在运行”或“已停止” - 上面的 Xpath 属于这个状态字段。

在执行此搜索后,如果状态为“运行”,那么我需要单击 Product1。

<form id="searchVO" action="/webpage/webpage/search-products?execution=e2s1" method="post">
<div id="nc-bd">
<h2>
Search results for
Some phone number
</h2>
</div>
<br>
<div>
<spring:eval expression="@webpageServicesProperties['SOMEVARIABLE_SWITCH']" var="someVariableSwitch">
<div id="errorMessage" style="display: none;">
<label class="error-message" id="errorBar"></label>
</div>
<table style="width: 100%;">
<caption>Products </caption>
<tbody>
<tr>
<th>Name</th>
<th>Product</th>
<th>Some phone number</th>
<th>Username</th>
<th>Status</th>
</tr>
<tr>
<td>Mr XXX</td>
<td>
<a href="CustomerID=333333333">Product1</a>
</td>
<td>
09876543210
</td>
<td class="wrap-txt">someemail@ID.com</td>
<td>Operative</td>
</tr>
<tr>
<td>Mr XXX</td>
<td>
<a href="CustomerID=4444444444">Product2</a>
</td>
<td>
09876543210
</td>
<td class="wrap-txt">someemail@ID.com</td>
<td>Stopped</td>
</tr>
</tbody>
</table>
</spring:eval></div>
</form>

请帮忙查找此问题的原因。

最佳答案

这是运算符(operator)的 xpath。

WebElement formElement = wd.findElement(By.xpath("//div[@id='nc-bd']/following-sibling::div[1]//table//tr[2]//td[5]"));

并且 xpath 已停止。

WebElement formElement = wd.findElement(By.xpath("//div[@id='nc-bd']/following-sibling::div[1]//table//tr[3]//td[5]"));

关于java - Selenium |命名空间错误: Failed to execute 'evaluate' on 'Document' | Contains unresolvable namespaces,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56153397/

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