Home > Net >  Selenium InvalidSelectorException with XPath only on Linux
Selenium InvalidSelectorException with XPath only on Linux

Time:02-18

I wrote tests using Selenide on Windows, the test passes, but when I run the same one on jenkins (Linux) I get an error:

org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression \button[@] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '\button[@]' is not a valid XPath expression.

the logs indicate the problem that we only have one "/". In contrast, the selector in the code looks like this:

private final SelenideElement addItemsBtn = $(By.xpath("//button[@class=\"ui-button ui-button-text f-mode-link f-section-button section-add-button ng-star-inserted\"]"));

the problem is slash, which is misinterpreted on Linux. The workaround for this is to replace XPath in favor of cssSelectors where there is no slash, or to change xPath to:

private final SelenideElement addItemsBtn = $(By.xpath("\\\button[@class=\"ui-button ui-button-text f-mode-link f-section-button section-add-button ng-star-inserted\"]"));

but that is not the solution to the problem.

CodePudding user response:

Try like this

"\button[@class='ui-button ui-button-text f-mode-link f-section-button section-add-button ng-star-inserted']"

CodePudding user response:

Instead of

'\button[@]'

Your XPath expression should be

'//button[@]'

The // before the element tag name, here button means that this element can be located anywhere below the root node.
The \ in "//button[@class=\"ui-button ui-button-text f-mode-link f-section-button section-add-button ng-star-inserted\"]" expression is an escape backslash used there since you are using " to enclose the XPath expression with and you are using another " inside that string. So, in order to not indicate those " inside the string as end or beginning of the string you are using escape / there. Alternatively you could write this XPath instead of

"//button[@class=\"ui-button ui-button-text f-mode-link f-section-button section-add-button ng-star-inserted\"]"

To be

"//button[@class='ui-button ui-button-text f-mode-link f-section-button section-add-button ng-star-inserted']"

In this case no need to use escape \, just use ' instead of "

  • Related