Java 如何在SeleniumWeb驱动程序中写入等待,直到在下拉列表中填充值,然后单击按钮

Java 如何在SeleniumWeb驱动程序中写入等待,直到在下拉列表中填充值,然后单击按钮,java,selenium,Java,Selenium,在我的应用程序中,当我打开一个页面时,会显示一个下拉列表,然后我需要单击“继续”按钮。问题是下拉列表需要一些时间来加载值,但在我的代码中,它会在下拉列表加载之前单击。我尝试使用隐式等待和thread.sleep,但它有时工作,有时不工作。 代码: 您应该使用wait()和notify()方法。 在您编写的Thread.sleep()方法的位置,将其替换为this.wait()。 完成下拉列表的加载数据的位置放在那里this.notify()method。 我希望这会对你有所帮助。像这样的东西应该

在我的应用程序中,当我打开一个页面时,会显示一个下拉列表,然后我需要单击“继续”按钮。问题是下拉列表需要一些时间来加载值,但在我的代码中,它会在下拉列表加载之前单击。我尝试使用隐式等待和thread.sleep,但它有时工作,有时不工作。 代码:

您应该使用
wait()
notify()
方法。 在您编写的
Thread.sleep()
方法的位置,将其替换为
this.wait()
。 完成下拉列表的加载数据的位置放在那里
this.notify()
method。
我希望这会对你有所帮助。

像这样的东西应该能满足你的需要

for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { 
        Select droplist = new Select(driver.findElement(By.Id("selection")));
        if(!droplist.getOptions().isEmpty()){
            break;
        }
    } catch (Exception e) {
         // best put something here
    }
    Thread.sleep(1000);
}
你可以用

final Select droplist=new Select(driver.findElement(By.Id(“selection”));
新FluentWait(驱动程序)
.带超时(60,时间单位。秒)
.pollingEvery(10,时间单位为毫秒)
.until(新谓词(){
公共布尔应用(WebDriver d){
返回(!droplist.getOptions().isEmpty());
}
});

您可以像下面这样使用:

//To type text in drop down 
driver.findElement(By.id("ur id")).sendKeys("txt");

//Use implicit wait to load the drop down
driver.manage().timeouts().implicitlyWait(250, TimeUnit.MILLISECONDS)

//Then click on the value in the drop down
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[@class='ui-menu-item'][5]"))).click()

//Now click the next drop down after clicking the drop down value
driver.findElement(By.className("buttonname")).click()

不久前我也遇到了同样的问题。这是我使用Java 8的解决方案:

    void selectTextFromDropDown(final By locator, final String value, final int timeoutInSeconds) {
    FluentWait<WebDriver> wait = createWait(timeoutInSeconds);
    wait.until(input -> {
        Select mySelect = new Select(input.findElement(locator));
        List<WebElement> options = mySelect.getOptions();
        for (WebElement option : options) {
            if (option.getText().equalsIgnoreCase(value)) {
                option.click();
                mySelect.getAllSelectedOptions().contains(value.toLowerCase());
                break;
            }
        }
        return true;
    });
}
void selecttextfrom下拉列表(按定位器最终、最终字符串值、最终int timeoutin秒){
FluentWait=createWait(timeoutInSeconds);
等待.直到(输入->{
Select mySelect=newselect(input.findElement(locator));
List options=mySelect.getOptions();
用于(WebElement选项:选项){
if(option.getText().equalsIgnoreCase(值)){
选项。单击();
mySelect.getAllSelectedOptions().contains(value.toLowerCase());
打破
}
}
返回true;
});
}

在这里,使用正确的X路径进入选项是一个简单的解决方案。请尝试以下代码

WebDriverWait wait=newwebdriverwait(驱动程序,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(//select[@name='Location']]/option[@value='1'])


这将查看选项中的元素是否已加载,如果未加载,则将等待它加载到DOM中,等待给定的秒数。

Hi BenvnQ,我尝试了此操作,但在代码页上出现以下警告“方法失败(字符串)未定义类型Home”.Home是我的类名。fail是junit提供的静态断言方法。您可以执行静态导入来获取它。如果您不使用junit,可以替换为运行时错误。Karna,我也在尝试使用fluent wait,但代码在“return(!droplist.getOptions().isEmpty());“@Huma:只需将
droplist
标记为final”行附近显示此错误“无法引用在其他方法中定义的内部类中的非final变量droplist”。我已经把它添加到我的answer@Huma:很高兴我能帮忙!
//To type text in drop down 
driver.findElement(By.id("ur id")).sendKeys("txt");

//Use implicit wait to load the drop down
driver.manage().timeouts().implicitlyWait(250, TimeUnit.MILLISECONDS)

//Then click on the value in the drop down
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[@class='ui-menu-item'][5]"))).click()

//Now click the next drop down after clicking the drop down value
driver.findElement(By.className("buttonname")).click()
    void selectTextFromDropDown(final By locator, final String value, final int timeoutInSeconds) {
    FluentWait<WebDriver> wait = createWait(timeoutInSeconds);
    wait.until(input -> {
        Select mySelect = new Select(input.findElement(locator));
        List<WebElement> options = mySelect.getOptions();
        for (WebElement option : options) {
            if (option.getText().equalsIgnoreCase(value)) {
                option.click();
                mySelect.getAllSelectedOptions().contains(value.toLowerCase());
                break;
            }
        }
        return true;
    });
}