从3个下拉列表中选择项,并依赖第一个

时间:2018-11-19 08:20:45

标签: java selenium-webdriver selenium-chromedriver

我想做的是: 1-转到该站点:http://www.emlakyonetim.com.tr/tr-TR/sitelerimiz, 2-单击第一个下拉菜单-单击一个城市,单击第二个下拉菜单-单击县,然后单击第三个下拉菜单。

简而言之,第二个下拉菜单项依赖于第一个,而第三个下拉菜单项依赖于第二个。

代码(不完整):

@Test
public void SiteCek() throws InterruptedException
{
    driver.get("http://www.emlakyonetim.com.tr/tr-TR/sitelerimiz");
    Thread.sleep(2000);

    driver.findElement(By.id("select2-city-list-container")).click();

    List<WebElement> elm = driver.findElements(By.xpath("//*[@class='select2-results__option']"));

    for(int i = 1; i < elm.size(); i++) 
    {   
            By ID = By.id(driver.findElements(By.xpath("//*[@class='select2-results__option']")).get(i).getText());
            System.out.println(ID);         

            driver.findElements(By.xpath("//*[@class='select2-results__option']")).get(i).click();
            Thread.sleep(500);
    } 
}

我在城市“ ADANA”后收到java.lang.IndexOutOfBoundsException:索引:2,大小:0错误。 如果我设法处理第一个错误,我将编写第二个和第三个for循环,因此代码现在是这样。

问题解决后,我想首先到达所有城市。然后是每个城市的县。然后是每个县的遗址。必须根据城市列表,县列表和站点列表的大小动态地完成此操作。为此,我需要三个嵌套的for循环。毕竟,每个值都必须用excel编写。

1 个答案:

答案 0 :(得分:1)

这是对我有用的代码

 public void testMethod() {
    driver.manage().window().maximize();

    WebElement firstDropDown = driver.findElement(By.id("select2-city-list-container"));
    firstDropDown.click();
    sleep();
    List<WebElement> citiesEls = getCitiesEls();
    Map<String, Map<String, List<String>>> cityData = new HashMap<>();
    for (int i = 0; i < citiesEls.size(); i++) {
        //we need to take this element every iteration, because it gets reloaded every time we open the dropdown
        WebElement cityEl = driver.findElement(By.id("select2-city-list-results")).findElements(By.xpath("//*[contains(@id,'select2-city-list-result')]")).get(i);
        String cityText = cityEl.getText();
        cityEl.click();
        sleep();

        cityData.put(cityText, getRegions());

        firstDropDown.click();
        sleep();
    }

    System.out.println(cityData);
}

private Map<String, List<String>> getRegions() {
    WebElement secondDropDown = driver.findElement(By.id("select2-region-list-container"));
    secondDropDown.click();
    sleep();
    List<WebElement> regionsEls = getRegionEls();
    Map<String, List<String>> regionData = new HashMap<>();
    for (int i = 0; i < regionsEls.size(); i++) {
        WebElement regionEl = driver.findElement(By.id("select2-region-list-results")).findElements(By.xpath("//*[contains(@id,'select2-region-list-result')]")).get(i);
        String regionText = regionEl.getText();
        regionEl.click();
        WebElement thirdDropDown = driver.findElement(By.id("select2-site-list-container"));
        thirdDropDown.click();
        List<WebElement> sitesEl = getSiteEls();
        List<String> sitesTexts = getSites(sitesEl);
        //populate region data
        regionData.put(regionText, sitesTexts);

        secondDropDown.click();
        sleep();
    }
    return regionData;
}

private List<String> getSites(List<WebElement> sitesEl) {
    List<String> sitesTexts = new ArrayList<>();
    for (WebElement siteEl : sitesEl) {
        sitesTexts.add(siteEl.getText());
    }
    return sitesTexts;
}

private List<WebElement> getSiteEls() {
    WebElement ulSites = driver.findElement(By.id("select2-site-list-results"));
    return ulSites.findElements(By.xpath("//*[contains(@id,'select2-site-list-result')]"));
}

private List<WebElement> getRegionEls() {
    return driver.findElement(By.id("select2-region-list-results")).findElements(By.xpath("//*[contains(@id,'select2-region-list-result')]"));
}

private List<WebElement> getCitiesEls() {
    return driver.findElement(By.id("select2-city-list-results")).findElements(By.xpath("//*[contains(@id,'select2-city-list-result')]"));
}

由于每次点击后数据都会动态更改,因此每次点击后可能需要一些延迟。下面的代码在Mac + Chrome上运行良好。不过,如果从头开始失败,请在每次单击后添加一个sleep方法调用。

  private void sleep() {
      try {
          Thread.sleep(1000);
      } catch (InterruptedException e) {
          throw new IllegalStateException(e);
      }
    }

重要说明:不建议使用睡眠,而应仅将其用作快速的临时解决方法。更可靠的解决方案是使用smart waits