SyntaxError: Failed to execute 'evaluate' on 'Document' error passing parameter as xpath?

SyntaxError: Failed to execute 'evaluate' on 'Document' error passing parameter as xpath?

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

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

testsuite.xml 中的行如下所示:

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

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

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

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

是否可以通过参数来实现?

这个错误信息...

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

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

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

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

参考资料

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