【问题标题】:SyntaxError: Failed to execute 'evaluate' on 'Document' error passing parameter as xpath?SyntaxError:无法在“文档”上执行“评估”错误,将参数作为 xpath 传递?
【发布时间】:2021-02-09 21:22:51
【问题描述】:

我正在尝试将 xpath 作为参数传递(redirectButton):

WebElement zoomLogo = driver.findElement(By.xpath(redirectButton));

testsuite.xml 中的行如下所示:

            <parameter name="redirectButton" 
        value="&quot;//img[@alt='zoom logo colour long']&quot;)" />

但是当我尝试使用 testng 通过 testsuite 文件运行测试时,它显示以下错误:

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '"//img[@alt='zoom logo colour long']")' is not a valid XPath expression.

我确信我有正确的 xpath,因为当我将它直接粘贴到代码中时,一切正常,但是当我尝试通过参数执行它时它不起作用。

可以通过参数来实现吗?

【问题讨论】:

  • testsuite.xml 中的行应该是&lt;parameter name="redirectButton" value="//img[@alt='zoom logo colour long']" /&gt;(没有&amp;quot; 和最后的)),因为xpath 本身是//img[@alt='zoom logo colour long']。如果您在 Java 中将 xpath 编写为 String,则必须将其放在引号中,但这是在 Java 中定义字符串的方式,而不是 xpath 表达式的一部分。

标签: java selenium selenium-webdriver syntax-error testng


【解决方案1】:

此错误消息...

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '"//img[@alt='zoom logo colour long']")' is not a valid XPath expression.

...暗示您使用的 不是有效的XPath 表达式。

看来你快到了。您需要删除两个 &amp;quot; 字符和额外的 ) 以使其成为有效的 xpath 表达式,如下所示:

<parameter name="redirectButton" value="//img[@alt='zoom logo colour long']" />

参考文献

您可以在以下位置找到一些相关的详细讨论:

【讨论】:

  • 感谢您的帮助和解释!!
猜你喜欢
  • 2018-10-07
  • 2021-02-19
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 2019-02-03
  • 1970-01-01
  • 2019-12-25
  • 2014-02-14
相关资源
最近更新 更多