0

Need your help in locating proper XPath for findElements method in Selenium. Below are the details:

URL - http://www.cleartrip.com/hotels/info/hotel-royal-heritage-30km-before-mount-abu-713374/

From the above URL, I want to extract only the below headers available on right hand side of the page.

1) Basic Amenities
2) Food & Beverages
3) Travel
4) Personal Services
5) Other Amenities

I have tried below XPaths till now:

1) html/body/div[1]/div[4]/div[2]/* This extracts everything along with amenities listed under the headers.

2) html/body/div[1]/div[4]/div[2]/h3/* this doesn't work.

3) html/body/div[1]/div[4]/div[2]/h[*] this doesn't work either.

Any ideas please?

Thanks, Bharat.

3 Answers 3

1

You can try these

//*[@class='col col8']/h3
//*[@class='hotelInfo row']/div[2]/h3  

You can iterate using this:

List<WebElement> expected = driver.findElements(By.xpath("//*[@class='col col8']/h3"));
        for (int i=0; i<expected.size(); i++){
                System.out.println(expected.get(i).getText());
        }

Which prints

Basic amenities
Food & Beverage
Travel
Personal Services
Other Amenities
Hotel Amenities Basic
Room Amenities

Sign up to request clarification or add additional context in comments.

2 Comments

I swear this is not coincidence....I found the answer as well....Below xpath I used...html/body/div[1]/div[4]/div[2]/h3
both of those xpaths are pretty ambiguous in that its not clear by reading either of them what they are selecting.
0

Why not store all the elements in a list?

List<WebElement> list = getDriver().findElements(By.xpath(.//*[@id='HotelTabs']/li));

This will make it easy for you to iterate over the headers and do what you need to do.

3 Comments

Hi cathal, Not sure wht exactly you mean? How would I then iterate over the headers? Why can't simply do that now? instead iterating over it later?
The above xpath will store find every element which it matches and store each element in a the list "list", you can then go through that as you would any other list. Other wise you can create an individual WebElement for each of the headers by appending its index to the above xpath, ie .//*[@id='HotelTabs']/li[1] == Overview
@cathal - Bharat is asking to locate texts 1) Basic Amenities 2) Food & Beverages 3) Travel 4) Personal Services 5) Other Amenities which appears on right hand side. You need to scroll page and locate this texts. The one your xpath is locating is different.
0

Below is xpath which can help you to identify the expected webelements.

Xpath: (//*[contains(@class,'amenitiesDescription ')]/div)[3]/following-sibling::div/strong

-> Strategy Used -- Structure based locator (i.e. xpath) -- tried to identified an element first and later tried to get the below siblings. -- Added all the identified webelements to a list. -- Used a for-each loop to iterate and get the inner text from the element and print on the console.

List<WebElement> eleList = driver.findElements(By.xpath("(//*[contains(@class,'amenitiesDescription ')]/div)[3]/following-sibling::div/strong");

for(WebElement ele: eleList){
     System.out.println(ele.getText());

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.