Reputation: 1027
Selenium: I have to Select value from a drop down which is dependent on value selected in another drop down.
Ex: I have two drop downs 1 and 2. Value to be populated in 2 is dependent on 1. When I select value in dropdown 1 then page gets refreshed and value in 2 is populated. I have to select value in drop down 2.
I receive error Element is no longer attached to DOM
.
I tried using wait.until((ExpectedCondition<Boolean>) new ExpectedCondition<Boolean>()
but it does not help me. Same issue occurs.
I tried using WebElement
and Select
but neither helped. Can anyone help me figure out solution?
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);
I am using waitFor(2000)
a defined function for waiting for specified timeperiod.
Upvotes: 3
Views: 4203
Reputation: 302
These are the functions you need. This will help you so the test cases will not fail due to page changing during testing. Such a population of a select tag.
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");
}
Upvotes: 1
Reputation: 22720
Element no longer attached...
mostly appears if page is refreshed and you are trying to perform any operation on previously created webElement object.
Here page might be refreshed on selection of first drop down and looks like you are performing select operation on first drop down web element instead of second.
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
For more details: Random "Element is no longer attached to the DOM" StaleElementReferenceException
Upvotes: 1