【问题标题】:Selenium | NamespaceError: Failed to execute 'evaluate' on 'Document' | Contains unresolvable namespaces硒 |命名空间错误:无法对“文档”执行“评估”|包含无法解析的命名空间
【发布时间】:2019-05-15 15:57:09
【问题描述】:

我可以使用如下浏览器的开发者工具检查元素:

使用 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;但是,上面的元素存在于表单中——下面给出了这个元素的 sn-p——我试图找到下面的状态“Operative”或“Stopped”——上面的 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>

请帮忙找出这个问题的原因。

【问题讨论】:

  • 不确定我之前是否必须在 xpath 中处理命名空间...出于好奇,这行得通吗? WebElement formElement = wd.findElement(By.xpath("collection()//*[@id='searchVO']/div[2]/spring:eval/table/tbody/tr[2]/td[5]"));如果是这样我可以提供一种解释,但我没有办法测试它。
  • @mrfreester,感谢您的回复,我使用了与您相同的 Xpath 并得到以下异常:;我错过了吗? org.openqa.selenium.InvalidSelectorException: 无效选择器: 无法使用 xpath 表达式 collection()//*[@id='searchVO']/div[2]/spring:eval/table/tbody/tr[ 定位元素2]/td[5] 因为以下错误: SyntaxError: Failed to execute 'evaluate' on 'Document': The string 'collection()//*[@id='searchVO']/div[2]/spring :eval/table/tbody/tr[2]/td[5]' 不是有效的 XPath 表达式。
  • 试图让你的 xpath 接受所有命名空间是一件很困难的事情。我以前从未这样做过,所以它不起作用也就不足为奇了 :) 备用选择器工作得很好!我确信有一种方法可以包含使spring:eval 部分正常工作所需的命名空间。我猜这就是导致您的问题的原因。

标签: java selenium evaluate invalidselectorexception


【解决方案1】:

这里是执行的 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]"));

【讨论】:

  • 非常感谢 Kunduk,您的上述解决方案非常完美。 +1。您能否确认即使 iFrame 下存在子节点,[following-sibling] 是否也可以工作?
  • 是的,但你必须先切换到 iframe
【解决方案2】:

这是一个 xpath,它将用Operative 识别所有产品。

//form[@id='searchVO']//table//tr[td[normalize-space(.)='Operative']]/td[count(./ancestor::table//tr[1]/th[normalize-space(.)='Product'])+1]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多