Selenium:我必须从下拉列表中选择值,这取决于在另一个下拉列表中选择的值。
例如:我有两个下拉列表 1 和 2。要填充到 2 中的值取决于 1。当我在下拉列表 1 中选择值时,页面会刷新并填充 2 中的值。我必须在下拉列表 2 中选择值。
我收到错误 Element is no longer attached to DOM
.
我尝试使用 wait.until((ExpectedCondition<Boolean>) new ExpectedCondition<Boolean>()
但这对我没有帮助。发生同样的问题。
我尝试使用 WebElement
和 Select
但都没有帮助。谁能帮我想出解决办法?
JavascriptExecutor executor2 = (JavascriptExecutor)driver;
executor2.executeScript("arguments[0].click();", <elementname>);
waitFor(3000);
Select <objectname1>= new Select(driver.findElement(By.id("<ID_for_drop_down_1>")));
selectCourse.selectByVisibleText("<valuetobeselected>");
waitFor(2000);
Select <objectname2>= new Select(driver.findElement(By.id("ID_for_drop_down_2")));
selectCourse.selectByVisibleText("<valuetobeselected>");
waitFor(2000);
我正在使用 waitFor(2000)
用于等待指定时间段的已定义函数。
最佳答案
这些是您需要的功能。这将帮助您使测试用例不会因测试期间页面更改而失败。这样一个选择标签的人口。
public void selectByValue(final By by, final String value){
act(by, 3, new Callable<Boolean>() {
public Boolean call() {
Boolean found = Boolean.FALSE;
wait.until(ExpectedConditions.refreshed(ExpectedConditions.elementToBeClickable(by)));
Select select = new Select(driver.findElement(by));
select.selectByValue(value);
found = Boolean.TRUE; // FOUND IT
return found;
}
});
}
private void act(By by, int tryLimit, boolean mode, Callable<Boolean> method){
boolean unfound = true;
int tries = 0;
while ( unfound && tries < tryLimit ) {
tries += 1;
try {
wait.until(ExpectedConditions.refreshed(ExpectedConditions.visibilityOfElementLocated(by)));
unfound = !method.call(); // FOUND IT, this is negated since it feel more intuitive if the call method returns true for success
} catch ( StaleElementReferenceException ser ) {
logger.error( "ERROR: Stale element exception. ");
unfound = true;
} catch ( NoSuchElementException nse ) {
logger.error( "ERROR: No such element exception. \nError: "+nse );
unfound = true;
} catch ( Exception e ) {
logger.error( e.getMessage() );
}
}
if(unfound)
Assert.assertTrue(false,"Failed to locate element");
}
关于testing - Selenium:从下拉列表中选择值,该值取决于在另一个下拉列表中选择的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30184055/