'Selenium InvalidSelectorException with XPath only on Linux

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[@class="ui-button ui-button-text f-mode-link f-section-button section-add-button ng-star-inserted"] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '\button[@class="ui-button ui-button-text f-mode-link f-section-button section-add-button ng-star-inserted"]' 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.



Solution 1:[1]

Try like this

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vivek rajagopalan