Reputation: 428
I am practicing the Xpath creation on (any fight booking search engine - MMT as reference) after long time and have few queries related to dynamic xpath. My Query is below
//div[@class="clusterContent"]/div/div[3]/div/div[2]/div/div//following::div/p[2]
//div[@class='clusterContent']/div
will give me list of line items, will store this in List and i will iterate the below xpath till this size.
@FindBy(xpath="//div[@class='clusterContent']")
List<WebElement> totalNoFlight;
public void getFlightCount()
{
int size = totalNoFlight.size();
for(int i=0;i<size;i++)
{
WebElement videoCliplnk = driver
.findElement(By.xpath("//div[@class='clusterContent']/div/div["+i+"]/div/div[2]/div/div//following::div/p[2]"));
String videoLinkhrs = videoCliplnk.getText();
}
}
Is this a correct way to write these king of Dynamic xpaths in POM page factory design pattern?
Upvotes: 0
Views: 98
Reputation: 33384
To get the Airline name from search results, Use following xpath
.
//div[@class='makeFlex spaceBetween']//p[contains(@class, 'airlineName')]
Ideally you should use some delay to load the page first, Then use below code.
List<WebElement> totalNoFlight = driver.findElements(By.xpath("//div[@class='makeFlex spaceBetween']//p[contains(@class, 'airlineName')]"));
for(int i = 0; i < totalNoFlight.size(); ++i) {
system.out.println(totalNoFlight.get(i).getText());
}
Upvotes: 1
Reputation: 8478
//div[@class="clusterContent"]/div/div[3]/div/div[2]/div/div//following::div/p[2]
Above XPath expression is incorrect. It locates 144 elements(see screenshot below).
To locate/fetch the 3rd line of search results you can use below XPath expressions:
(//div[@class='makeFlex spaceBetween'])[3]
-- This will return the third row <div>
of search results. Just change last digit 3
for third row, 4
for fourth row and so on.
(//div[@class='makeFlex spaceBetween'])[3]//p
-- This will return the third row's all <p>
nodes.
Upvotes: 1
Reputation: 4829
Airline name seemed to have unique locator by class names.
Card locator is element that contains class listingCard
.
Name container locator is element that contains class airline-info-wrapper
.
And finally Airline name is element where one of classes is airlineName
.
So, you got a result:
//*[contains(@class,'listingCard ')]//*[contains(@class, 'airline-info-wrapper')]//*[contains(@class,'airlineName')]
If you don't care about containers root, it can be even shorter - just
//*[contains(@class,'airlineName')]
But if you care not only about name, you need get list of card containers //*[contains(@class,'listingCard ')]
, loop through it and get needed info.
Upvotes: 1