如何在下图中找到空城下拉菜单?

时间:2016-05-19 09:41:47

标签: java selenium selenium-webdriver selenium-ide selenium-rc

功能:我们有三个字段Country,State,City -

enter image description here

选择国家后,它会显示州名单,选择州后会显示城市列表。

问题:在这个州没有城市,所以如何找到国家,国家列出那些没有城市列表使用硒?

我尝试了下面的代码,但循环不起作用&不显示空列表

@Test
public void findstate() throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    driver.get("URL");
    Select country = new Select(driver.findElement(By.xpath("//*[@id='UserProfileCountryId']")));
    List<WebElement> countrylist = country.getOptions();
    System.out.println(countrylist.size());
    for (int i = 1; i <= countrylist.size(); i++) {
        countrylist.get(i).click();
        Select state = new Select(driver.findElement(By.xpath("//*[@id='UserProfileStateId']")));
        List<WebElement> statelist = state.getOptions();
        for (int j = 1; j <= statelist.size(); j++) {
            statelist.get(j).click();
            Select city = new Select(driver.findElement(By.xpath("//*[@id='UserProfileCityId']")));
            List<WebElement> citylist = city.getOptions();
            System.out.println("Country, state list of Empty Cities");
            if (citylist.size() > 1) {
                System.out.println(countrylist.get(i).getText() + "-----" + statelist.get(j).getText() + "-----");
            }

        }

    }

}

国家,州,城市的源代码:

<span class="select-wrapper"><select name="data[UserProfile][country_id]" class="custom-select valid" id="UserProfileCountryId" aria-invalid="false" aria-required="true">
<option value="">Country</option>
<option value="1">Afghanistan</option>
<option value="2">Albania</option>
</select><span class="holder">Afghanistan</span></span>
<select name="data[UserProfile][state_id]" class="custom-select valid" id="UserProfileStateId" aria-invalid="false" aria-required="true">
<option value="">State</option>
<option value="42">Badakhshan</option>
<option value="43">Badgis</option>
<option value="44">Baglan</option>
</select>
<select name="data[UserProfile][city_id]" class="custom-select valid" id="UserProfileCityId" aria-invalid="false" aria-required="true">
<option value="">City</option>
<option value="5916">Andarab</option>
<option value="5917">Baghlan</option>
</select>

4 个答案:

答案 0 :(得分:0)

我认为,由于复制和粘贴,你有一些拼写错误。

您需要在循环中使用正确的Select变量,但始终使用country

试试这个:

@Test
public void findstate() throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    driver.get("URL");
    Select country = new Select(driver.findElement(By.xpath("//*[@id='UserProfileCountryId']")));
    List<WebElement> countrylist = country.getOptions();
    System.out.println(countrylist.size());
    for (int i = 1; i <= countrylist.size(); i++) {
        countrylist.get(i).click();
        Select state = new Select(driver.findElement(By.xpath("//*[@id='UserProfileStateId']")));
        List<WebElement> statelist = state.getOptions();
        for (int j = 1; j <= statelist.size(); j++) {
            statelist.get(j).click();
            Select city = new Select(driver.findElement(By.xpath("//*[@id='UserProfileCityId']")));
            List<WebElement> citylist = city.getOptions();
            System.out.println("Country, state list of Empty Cities");
            if (citylist.size() > 1) {
                System.out.println(countrylist.get(i).getText() + "-----" + statelist.get(j).getText() + "-----");
            }

        }

    }

}

为了避免这个错误,最好像这样使用foreach循环:

for (WebElement element : country.getOptions()) { }

我不知道您的HTML代码,但在询问getOptions()之前,等待其他下拉菜单填充元素也可能。 Selenium非常快,它可能会在您的网站加载之前询问下拉菜单的选项。在这种情况下,即使使用更新的代码也不会得到任何结果。

每次都尝试等待下拉菜单填写:

    Select country = new Select(driver.findElement(By.xpath("//*[@id='UserProfileCountryId']")));
    List<WebElement> countrylist = country.getOptions();
    System.out.println(countrylist.size());
    for (WebElement element : countrylist) {
        element.click();
        Select state = new Select(driver.findElement(By.xpath("//*[@id='UserProfileStateId']")));

        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver webdriver) {
           return state.getOptions() > 1;
           }
        });

        List<WebElement> statelist = state.getOptions();
        ....

您对城市选项也这样做 - &gt;如果你遇到超时,你会发现没有城市的州......

答案 1 :(得分:0)

嗨,请尝试如下

@Test
public void findstate() throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    driver.get("URL");
    WebDriverWait wait = new WebDriverWait(driver,20);
    Select country = new Select(driver.findElement(By.xpath("//*[@id='UserProfileCountryId']")));
    List<WebElement> countrylist = country.getOptions();
    System.out.println(countrylist.size());
    for (int i = 0; i <= countrylist.size(); i++) {
        countrylist.get(i).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='UserProfileStateId']")));
        Select state = new Select(driver.findElement(By.xpath("//*[@id='UserProfileStateId']")));
        // here use state not country
        List<WebElement> statelist = state.getOptions();
        for (int j = 0; j <= statelist.size(); j++) {
            statelist.get(j).click();
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='UserProfileCityId']")));
            Select city = new Select(driver.findElement(By.xpath("//*[@id='UserProfileCityId']")));
         // here use city not country
            List<WebElement> citylist = city.getOptions();
            for (int k = 0; k <= citylist.size(); k++) {
                citylist.get(k).click();
                System.out.println("Country, state list of Empty Cities");
                if (citylist.get(k).getText() == null) {
                    System.out.println(countrylist.get(i).getText() + "-----" + statelist.get(j).getText() + "-----"+ citylist.get(k).getText());
                }
            }
        }
    }
}

答案 2 :(得分:0)

在最外层循环选择国家/地区,然后在内部循环中选择该国家/地区的每个州,并检查是否找到了城市。如果没有,那么得到城市&amp;国家和地区走这条路。

我认为,它可以帮助你。

答案 3 :(得分:0)

它工作正常,选择国家后添加'tab'键

driver.findElement(By.xpath("//*@id='UserProfileCountryId']")).sendKeys(Keys.TAB);

但是我在执行时遇到了另一个问题,经过一段时间后运行脚本我得到了错误 org.openqa.selenium.StaleElementReferenceException:元素不再附加到DOM 命令持续时间或超时:31毫秒

感谢您的回答