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)
定义的函数来等待指定的时间段。
答案 0 :(得分:1)
Element no longer attached...
。
在选择第一个下拉列表时,此页面可能会刷新,看起来您在第一个下拉Web元素而不是第二个下拉列表中执行选择操作。
Select dropDown1= new Select(driver.findElement(By.id("<ID_for_drop_down_1>")));
dropDown1.selectByVisibleText("<valuetobeselected>"); // Should be dropdown1
waitFor(2000);
// Page might be refreshed here
Select dropDown2= new Select(driver.findElement(By.id("ID_for_drop_down_2")));
dropDown2.selectByVisibleText("<valuetobeselected>"); // use dropdwon2 not dropdown1
有关详情:Random "Element is no longer attached to the DOM" StaleElementReferenceException
答案 1 :(得分:1)
这些是您需要的功能。这将帮助您,因此测试用例不会因测试期间的页面更改而失败。这样一个选择标签的人口。
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");
}