【发布时间】:2015-10-06 19:05:23
【问题描述】:
driver.findElement(By.xpath(sOptionPath)).click(); //this option button changes contents of page
Thread.sleep(4000);
WebElement combo=driver.findElement(By.xpath(sXpath));
Select dropdownvalue = new Select(combo);
dropdownvalue.selectByVisibleText(sText);
上面的代码工作正常,但如果我使用 wait 而不是 thread.sleep,我会得到 StaleElementReferenceException 异常。 这是我使用的 Fluent 等待:
Wait<WebDriver> newwait=new FluentWait<WebDriver>(driver).withTimeout(10, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS).ignoring(StaleElementReferenceException.class);
WebElement combo=newwait.until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath(sXpath));
}
});
这会找到组合框,但再次对组合框执行任何操作都会导致 NoSuchElement 或 statestate 异常。所以我也试过这个从组合框中选择值:
Wait<WebElement> elwait=new FluentWait<WebElement>(combo).withTimeout(10, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS).ignoring(StaleElementReferenceException.class,NoSuchElementException.class);
Boolean a=elwait.until(new Function<WebElement,Boolean>(){
@Override
public Boolean apply(WebElement arg0) {
Select dropdownvalue = new Select(arg0);
dropdownvalue.selectByVisibleText(sText);
return true;
}
});
这超时并且不起作用!
我怎样才能使这项工作,为什么它不工作和 thread.sleep 工作。为什么使用 Thread.sleep 是一种不好的做法?
【问题讨论】:
标签: java selenium-webdriver wait