Home > database >  Why is this not a valid xpath in Python (Selenium)?
Why is this not a valid xpath in Python (Selenium)?

Time:10-09

In Python (Spyder/Anaconda) I have:

ExcelIconXpath = "//*[contains(@id='pagePane0_divBlock0_')]"

ExcelIcon = driver.find_element(by=By.XPATH, value=ExcelIconXpath)

And I get the following error message:

Unable to locate an element with the xpath expression //*[contains(@id='pagePane0_divBlock0_')] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(@id='pagePane0_divBlock0_')]' is not a valid XPath expression.

CodePudding user response:

It's not a very good error message, is it? Your expression is incorrect because the contains() function requires two arguments and you have only supplied one. The value you have supplied is the result of the equality comparison

@id='pagePane0_divBlock0_'

which is a boolean; it doesn't make any sense to supply a boolean as the input to contains().

You need to decide whether you actually want to do an equality comparison or a substring (contains) comparison, and then use one or the other rather than mixing them up.

CodePudding user response:

The correct xpath expression would be

"//*[contains(@id, 'pagePane0_divBlock0_')]"

Here is a good intro into xpath: https://www.w3schools.com/xml/xpath_intro.asp

Of course, given you're not waiting on that element to load/be clickable, there might be other issues you might be facing (outside the scope of your original question).

  • Related