Tscott
Tscott

Reputation: 485

how to find xpath of an element based on a number?

The issue I'm having is that I'm trying to find the xPath of the first element on a webpage that has more than 5 retweets. I'm trying to test in Selenium WebDriver using a testNG file. The page in question is the the following:

http://example.crowdynews.com/crowdynews/usa/politics/

I know I can just right click and find the Firepath of the element for it, but I would like to make the code dynamic considering that the news site is a dynamic as well.

If anyone can let me know how to do that or that could point me to a comprehensive site that could teach me how to do that, I would be very appreciative as I have yet to locate something that.

Upvotes: 0

Views: 439

Answers (1)

har07
har07

Reputation: 89285

This is one possible XPath selector :

//b[@class='kudos-count' and number(.)>5]/ancestor::div[@class='item-wrap li-link']

brief explanation :

  • //b : find all b elements anywhere in the HTML document...
  • [@class='kudos-count' and number(.)>5] : ...where class attribute equals "kudos-count" and the content of the element, after converted to number, is greater than 5
  • /ancestor::div : from such b elements, return the ancestor div element...
  • [@class='item-wrap li-link'] : ...where class attribute equals "item-wrap li-link". The class indicates the div that contains the entire individual news

Upvotes: 2

Related Questions