How to Solve the Error "Not a Valid Xpath Expression"

How to solve the error Not a valid XPath expression

The reason you are seeing an error as not a valid XPath expression because you have exactly 2 issues in it as follows:

  • As you are passing the xpath within single quotes i.e. '' you can't use the same for the attribute values.
  • Ideally an xpath shouldn't end with a /
  • So your effective xpath will be either of the following:

    '//*[@id="app"]/article/div[2]/section/div[1]/div[5]/div/section[2]/div[2]/div[1]'

    or

    "//*[@id='app']/article/div[2]/section/div[1]/div[5]/div/section[2]/div[2]/div[1]"

Not a valid xpath expression issue in selenium

You have your ] and ) mixed up at the end.

//div[contains(@class, 'ant-notification-notice ant-notification-notice-closable ng-trigger ng-trigger-notificationMotion'])... see how you start with [ but end with ).

Instead, try:

//div[contains(@class, 'ant-notification-notice ant-notification-notice-closable ng-trigger ng-trigger-notificationMotion')]

text() is not a valid XPath expression

I think this should work here

driver.findElement(By.xpath("(//div[contains(@ng-class,'sale.Sale.WarrantyExtensionFunctionAvailable')]/text())[1]")).getText();

Getting error: is not a valid XPath expression. when trying to reference a field within service now

I think you are making it more complicated than it needs to be. The input element you have highlighted has an id tag, try grabbing it by that

assignmentGroup = web.find_element_by_id("sys_display.sc_req_item.assignment_group")

Rule of thumb is to always search for ID or Name first, those are usually the most reliable. If those aren't available you can go into Xpath or CSS Selector

inside my string got result in The string is not a valid XPath expression

The text in the option isn't clear to me if it's 3"-D-003-1101_01C03 or "3"-D-003-1101_01C03" (as it appears in the error).

As per the error it is an incorrect XPATH because the quotes are not closed properly:

//select[@name='LocationInspectionId']/option[text()="3"-D-003-1101_01C03"]

I am assuming your html might look something similar to this:

  <select name="LocationInspectionId">
<option value="1">"34"-D-003-1101_01C03</option>
<option value="2">"32"-D-003-1101_01C03</option>
<option value="3">"3"-D-003-1101_01C03</option>
<option value="4">"43"-D-003-1101_01C03</option>
<option value="5">"5"-D-003-1101_01C03</option>
</select>

Now, if you wanted to click on the 3rd option ("3"-D-003-1101_01C03) then you can do this:

driver.find_element(By.XPATH, '//select[@name="LocationInspectionId"]/option[text()=\'"3"-D-003-1101_01C03\']').click()

If the option was "3"-"D-003-1101_01C03", then do this:

driver.find_element(By.XPATH, '//select[@name="LocationInspectionId"]/option[text()=\'"3"-"D-003-1101_01C03"\']').click()

And for 3"-D-003-1101_01C03:

driver.find_element(By.XPATH, '//select[@name="LocationInspectionId"]/option[text()=\'3"-D-003-1101_01C03\']').click()

If the option is just 3-D-003-1101_01C03, then:

driver.find_element(By.XPATH, '//select[@name="LocationInspectionId"]/option[text()="3-D-003-1101_01C03"]').click()

Just make sure you correctly escape (\' or \") the quotes and you'll be fine.

Is not a valid XPath expression

As @John Gordon pointed out in his comment, you are missing a = between the @class and the value "_3IfUe" that you are trying to compare.

After fixing that, you need an @ before the crossorigin attribute name. Otherwise, it thinks you are looking for an element with that name.

It should be:

//div[@class = "_3IfUe"]/img[@crossorigin = "anonymous"]

Not a valid XPath expression trying to use div aria label contains

There's a typo in given xpath, which is invalid due to missing parenthesis. The following should work:

unfollowuser = wait.until(EC.element_to_be_clickable(
(By.XPATH, "//div[contains(@aria-label, 'Following') and @role='button']")))


Related Topics



Leave a reply



Submit