sprogissd
sprogissd

Reputation: 3075

How to write XPath based on elements in Chrome Dev Tools?

I am trying to get XPaths for underlined values in the image below. I have a list of cards which is always different, and I need to grab values from the inside of the highlighted card which has 'highlight' in its class.

When I grab the XPath through inspecting an element in Chrome, I get something like this //*[@id="rental-asset"]/div/div[1]/div/rdo-nav-card-asset[11]/div/div[1]/div/i which doesn't work for me since that 11 in brackets is always going to be a different value. How can I create an XPath for these elements based on their unique class?

Chrome Dev Tools

Upvotes: 0

Views: 1765

Answers (3)

yong
yong

Reputation: 13712

You can firstly find the highlight card and use it as the search context of those underline texts.

Code as following:

var activeCard = driver.findElement(By.css('rdo-nav-card-asset > div.rdo-card-highlight'));

// the text of 1rd underline
var title = activeCard.findElement(By.css('.panel-title')).getText();

// the text of 2rd underline
var title = activeCard.findElement(By.xpath('.//div[@class='rdo-card-metric'][1]'))
           .getText();

// the text of 3rd underline
var title = activeCard.findElement(By.css('.text-danger')).getText();

// the text of 4rd underline
var title = activeCard.findElement(By.xpath('.//div[@class='rdo-card-metric'][3]'))
           .getText();

Upvotes: 0

Usama Jamil
Usama Jamil

Reputation: 66

You can Split the xpath and iterate in the range they are expected to occur.

Suppose they have that value from 1-22 you can something like this. This is a python code.

str1 = '//*[@id="rental-asset"]/div/div[1]/div/rdo-nav-card-asset['
str2 = ']/div/div[1]/div/i'
for i in range(1, 22):
    str3 = str1 + str(i) + str2
    a = driver.find_element_by_xpath(str3).text
    print a

You can grab the other elements or data inside the loop.

Comment below if you have any confusion.

Upvotes: 0

MivaScott
MivaScott

Reputation: 1806

Here is how to get the first underlined thing. I think you should be able to take it from here.

//rdo-nav-card-asset/div[contains(@class,'panel') and contains(@class,'highlight')]/div[contains(@class,'panel-heading')]/div[contains(@class,'panel-title')]

Upvotes: 1

Related Questions