• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using JSoup to get element XPath  RSS feed

 
Karl Jürgenson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I recently started using JSoup library, which is HTML element parsing library. It has served fine so long, but now I have the following problem:

When I get my element, for example a link ( )

then I need to retrieve it's XPath or something that I can use with selenium. My objective is to get link's selector code (XPath preferred) and pass it to my selenium code.

Problem is, JSoup doesn't support getting element's XPath. Any suggestions?
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure how you imagine this working as any single element on a page is accessible via any number of XPath expressions.
The below could have:
//a[@href='blah']
//a[@title='foo']
and that's ignoring any XPaths based on relative position.

Maybe if it had an id you could then define a single XPath, but there isn't one.
 
Karl Jürgenson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I found out that CSS Selector could help me with my objective. But I found out that it has one disadvantage: When there are multiple elements with same id-s, then they both have same CSS selectors.

Is there some sort of workaround there, to identify those two (like indexing or something)?
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Complain to whoever wrote the HTML?
The id's are supposed to be unique.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!