How to solve the error "Not a valid XPath expression"
Asked Answered
V

1

2

In selenium on Java, im try to find an element and select it on a webpage but it keep getting the error:

The string '//*[@id='app']/article/div[2]/section/div[1]/div[5]/div/section[2]/div[2]/div[1]/' is not a valid XPath expression. 

How can I get it at all??

Varnado answered 15/6, 2018 at 13:25 Comment(3)
you have an extra '/' at the end, remove it and it's fine.Spatola
Can't you select div[1] directly with CSS selector?Lactalbumin
share the html for which you want to have xpath.Lotion
C
6

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]"
    
Cham answered 15/6, 2018 at 20:19 Comment(1)
Based chad dev, you just solved my problem. upvoted sir.Eyde

© 2022 - 2024 — McMap. All rights reserved.