如何使用Selenium Java Webdriver搜索/查找所有结果

时间:2012-09-20 07:12:22

标签: selenium webdriver selenium-webdriver xpath

亲爱的Selenium Webdriver专家,

我是这个框架的新手,需要一些关于如何从销售房产列表网站查找/查找所有搜索结果的建议。以下是工作代码 已成功使用Selenium Webdriver找到所有属性,但我不知道如何使用findElements(by ...)来获取返回的每个结果 这个网站:

    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;        

有人可以就此查询提供一些建议吗?我还想在查找/查找所有返回的结果之前包含一个暂停机制?

非常感谢,

杰克


非常感谢Santoshsarma的回复,但我很难将您的建议应用到以下返回的网页查询页面输出片段中:

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

列出AllSearchResults = driver.findElements(By.id(“ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_hypAddress”));

ctl01中有一个索引需要循环才能获得所有搜索结果,这些搜索结果会因其他类似威胁而变慢。有没有更好的方法在WebElement上使用findElement()来搜索它的子项,但是我找不到找到根的位置。你能否确认这种方法是否有效?也许pin指向以下URL查询结果页面的结果列表的根目录: 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

我非常感谢你对我在这方面缺乏经验的耐心。

3 个答案:

答案 0 :(得分:0)

findElements方法将返回具有相同定位符的Web元素列表。

> List<WebElement> AllSearchResults=driver.findElements(By.id("value"));

在循环中运行上面的列表以获取每个搜索结果。

  for(WebElement eachResult:AllSearchResults)
   {
           eachResult.click(); 
   }

答案 1 :(得分:0)

如果仍未解决此问题,请尝试以下方法查找h3链接

(new WebDriverWait(driver, 10)).until(ExpectedConditions
   .visibilityOfElementLocated(
   By.id("ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_hypAddress")));

此代码等待给定元素10秒钟,然后抛出TimeOutException。

此致,基督徒

答案 2 :(得分:0)

我们可以使用ID属性在自定义xpath的帮助下解决它。

代码是这样的:

List AllSearchResults=driver.findElements(By.xpath("//*[contains(@id='SrchResLst_rptResult')]"));

或者像这样:

List AllSearchResults=driver.findElements(By.xpath("//*[contains(@id='ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_')]"));

我们在这里使用*,因为某些搜索结果包含标记<a><dd>

的组合

xpath搜索包含&#34; SrchResLst_rptResult&#34;的所有元素。 string作为ID属性的值。

相关问题