'How to search / find all results using Selenium Java Webdriver
Dear Selenium Webdriver Specialists,
I am new to this framework and needed a bit of advice on how to locate / find all the search results from a sales property listing website. Below is a working code which has successfully found all the properties using Selenium Webdriver but I don't know how to use findElements(by...) to pick up every results returned by this website:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.domain.com.au/?mode=buy");
// Enter the query string "3000"
WebElement query = driver.findElement(By.xpath(".//*[@id='ctl00_SearchMoreCriteria_Radar_searchToBuy']"));
query.sendKeys("3000");
WebElement searchButton = driver.findElement(By.xpath(".//*[@id='ctl00_SearchMoreCriteria_Radar_Search']"));
searchButton.click();
// Sleep until the div we want is visible or 5 seconds is over
long end = System.currentTimeMillis() + 5000;
Could anyone offer some advice on this query? I also like to include a pausing mechanism before locating / finding all the returned results?
Many thanks,
Jack
Thank you Santoshsarma very much for your response but I am struggling to apply your suggestion onto the following returned web query page output snippet:
<h3>
<a id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_hypAddress" href="/Property/For-Sale/Penthouse/VIC/Melbourne/?adid=2009775619">602/73 Flinders Lane, Melbourne</a></h3>
<dl class="cN-featDetails">
<dt class="proptype">Property type</dt>
<dd id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_ddPropertyType" class="propertytype type-house" title="Property type: House">House</dd>
<dt class="bedrooms">Bedrooms</dt>
<dd id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_ddBedrooms" class="bedrooms" title="Bedrooms">3</dd>
<dt class="bathrooms">Bathrooms</dt>
<dd id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_ddBathrooms" class="bathrooms" title="Bathrooms">4</dd>
<dt class="carspaces">Car spaces</dt>
<dd id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_ddCarSpaces" class="carspaces" title="Car spaces">2</dd>
</dl>
</div>
<div class="main-wrap">
<ul class="photo cfix">
<li>
<a id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_hypMainThumb" tabindex="-1" class="contain" href="/Property/For-Sale/Penthouse/VIC/Melbourne/?adid=2009775619"><img id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_imgMainThumb" title="602/73 Flinders Lane, Melbourne" src="http://images.domain.com.au/img/2012625/18127/2009775619_1_PM.JPG?mod=120925-150000" alt="Main photo of 602/73 Flinders Lane, Melbourne - More Details" style="border-width:0px;" /></a>
</li>
<li class="last">
<a id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_hypSecondThumb" tabindex="-1" class="contain" href="/Property/For-Sale/Penthouse/VIC/Melbourne/?adid=2009775619"><img id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_imgSecondThumb" title="602/73 Flinders Lane, Melbourne" src="http://images.domain.com.au/img/2012625/18127/2009775619_2_PM.JPG?mod=120913-125823" alt="Photo of 602/73 Flinders Lane, Melbourne - More Details" style="border-width:0px;" /></a>
</li>
</ul>
List AllSearchResults=driver.findElements(By.id("ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_hypAddress"));
There is an index in ctl01 that needs to cycle through to get all search results which would be slow according other similar threats. Is there a better approach to use findElement() on a WebElement to search just its children but I am at a lost on where to locate the root. Are you able to confirm whether this approach would work & perhaps pin point where the root of the list of results from the following URL query result page: http://www.domain.com.au/Search/buy/Property/Types/Apartment-Unit-Flat/Duplex/House/New-Apartments-Off-the-Plan/New-Home-Designs/New-House-Land/Penthouse/Semi-Detached/Studio/Terrace/Townhouse/Villa/State/VIC/Area/Inner-City/Region/Melbourne-Region/Suburb/Melbourne/?bedrooms=1&bathrooms=1&carspaces=1&from=450000&searchterm=3000&pois=PriSchl|1|2|2000
Your patience for my lack of experience in this area would be very much appreciated.
Solution 1:[1]
findElements method will return List of WebElements which has same locator.
> List<WebElement> AllSearchResults=driver.findElements(By.id("value"));
Run the above list in loop to get each individual search result.
for(WebElement eachResult:AllSearchResults)
{
eachResult.click();
}
Solution 2:[2]
If this is still unresolved, try the following for locating the h3 link
(new WebDriverWait(driver, 10)).until(ExpectedConditions
.visibilityOfElementLocated(
By.id("ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_hypAddress")));
This code waits for the given element for 10 seconds and then throws a TimeOutException.
Regards, Christian
Solution 3:[3]
May be we can solve it with the help of a custom xpath using ID property.
Code goes like this:
List AllSearchResults=driver.findElements(By.xpath("//*[contains(@id='SrchResLst_rptResult')]"));
or like this:
List AllSearchResults=driver.findElements(By.xpath("//*[contains(@id='ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_')]"));
Here we used * because some search results are having a combination of tags <a> and <dd>
The xpath searches for all the elements which is containing "SrchResLst_rptResult" string as a value of ID property.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Santoshsarma |
| Solution 2 | kleopatra |
| Solution 3 | Praveen |
