Rahil Kumar
Rahil Kumar

Reputation: 428

Looking for précised/Efficient Dynamic Xpath

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

  1. Have written a xpath to find the airlinename from list of LineItem. Below is Xpath which is returning 3rd line item details. Is this efficient or précised to fetch the the 3rd line item?

//div[@class="clusterContent"]/div/div[3]/div/div[2]/div/div//following::div/p[2]

  1. To fetch all the line item I have written the xpath as below

//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?

enter image description here

Upvotes: 0

Views: 98

Answers (3)

KunduK
KunduK

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

Shawn
Shawn

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).

enter image description here

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

Yaroslavm
Yaroslavm

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

Related Questions