0

亲爱的 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 的回复,但我正在努力将您的建议应用到以下返回的 Web 查询页面输出片段中:

     <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() 来搜索它的孩子,但我不知道在哪里找到根。您是否能够确认这种方法是否有效?也许可以从以下 URL 查询结果页面确定结果列表的根:http: //www.domain.com.au/Search/buy/Property/Types/公寓单元公寓/复式/独立屋/新公寓楼花/新住宅设计/新房子土地/阁楼/半独立/工作室/露台/联排别墅/别墅/州/VIC/区域/内城/地区/墨尔本地区/郊区/墨尔本/?bedrooms=1&bathrooms=1&carspaces=1&from=450000&searchterm=3000&pois=PriSchl|1|2|2000

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

4

3 回答 3

0

findElements 方法将返回具有相同定位器的 WebElements 列表。

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

循环运行上述列表以获取每个单独的搜索结果。

  for(WebElement eachResult:AllSearchResults)
   {
           eachResult.click(); 
   }
于 2012-09-20T16:41:07.350 回答
0

如果仍然无法解决,请尝试以下方法查找h3链接

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

此代码等待给定元素 10 秒,然后引发 TimeOutException。

问候, 克里斯蒂安

于 2013-01-16T10:01:23.683 回答
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 搜索包含“SrchResLst_rptResult”字符串作为 ID 属性值的所有元素。

于 2014-06-14T07:50:28.273 回答