Reputation: 703
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.
Upvotes: 0
Views: 268
Reputation: 78
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());
}
Upvotes: 0
Reputation: 1338
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.
Upvotes: 0
Reputation: 10877
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
Upvotes: 1