Nesting dropdowns using for loop is giving stale element reference error

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Nesting dropdowns using for loop is giving stale element reference error



I want to know how to nest dropdowns using selenium webdriver using java,i.e., I have 2 dropdowns and can these dropdowns be nested one after the other?
After looping 2 times for a dropdown it is showing stale element reference error



I have written the following code:


Select drpdwns6 = new Select(driver.findElement(By.xpath("//*[@id="MainContent_ddlBillable"]")));
List <WebElement> sels6 = drpdwns6.getOptions();
sels6.size();
for(int s6=0;s6<sels6.size();s6++) {
drpdwns6.selectByIndex(s6);
System.out.println("selected value"+s6);

Select drpdwns7 = new Select(driver.findElement(By.xpath("//*[@id="MainContent_ddlofflinestatus"]")));
List <WebElement> sels7 = drpdwns7.getOptions();
sels7.size();
for(int s7=0;s7<sels7.size();s7++) {
drpdwns7.selectByIndex(s7);
System.out.println("selected value"+s7);
}
}




2 Answers
2



My guess is selecting the option from the dropdown refreshes the DOM, so the exception is thrown. You need to relocate the dropdown in each itreation


Select drpdwns6 = new Select(driver.findElement(By.id("MainContent_ddlBillable")));
int drpdwns6Size = drpdwns6.getOptions().size();
for(int s6 = 0 ; s6 < drpdwns6Size ; s6++) {
drpdwns6 = new Select(driver.findElement(By.id("MainContent_ddlBillable")));
drpdwns6.selectByIndex(s6);
System.out.println("selected value"+s6);

Select drpdwns7 = new Select(driver.findElement(By.id("MainContent_ddlofflinestatus")));
int drpdwns7Size = drpdwns7.getOptions().size();
for(int s7 = 0 ; drpdwns7Size ; s7++) {
drpdwns7 = new Select(driver.findElement(By.id("MainContent_ddlofflinestatus")));
drpdwns7.selectByIndex(s7);
System.out.println("selected value"+s7);
}
}



As a side note, if you have an id use By.id it instead of By.xpath


id


By.id


By.xpath





Though it's working, but I have a question, why haven't you used List <webElement> ?
– Himanshu Bansal
Jul 24 at 13:18


<webElement>





@HimanshuBansal You can split the line int drpdwns6Size = drpdwns6.getOptions().size(); to two lines List <WebElement> sels6 = drpdwns6.getOptions(); int drpdwns6Size = sels6.size();. If you like, IMHO it isn't necessary. Just don't forget to assign the size() value to variable.
– Guy
Jul 24 at 13:22




int drpdwns6Size = drpdwns6.getOptions().size();


List <WebElement> sels6 = drpdwns6.getOptions(); int drpdwns6Size = sels6.size();


size()



You get the Stale element exception whenever the element present in the DOM is deleted or removed or is not available.



The above answer (ie) relocating the element once the DOM is refreshed or you could use Webdriver wait, If an element is not attached to DOM then you could try using ‘try-catch block’ within ‘for loop’ like below


driver.manage().timeouts().implicitlywait(30,TimeUnit.SECONDS);
try{
Select drpdwns6 = new
Select(driver.findElementByXpath("//[@id="MainContent_ddlBillable"]")));
List <WebElement> sels6AllOptions = drpdwns6.getOptions();
int count1=sels6AllOptions.size();
for(int s6=0;s6<count1;s6++)
{
drpdwns6.selectByIndex(s6);
}
}
catch(StaleElementException e1){
System.out.println("selected value"+s6);
}
try{
Select drpdwns7 = new Select(driver.findElement(By.xpath("//*[@id="MainContent_ddlofflinestatus"]")));

List <WebElement> sels7AllOptions = drpdwns7.getOptions();
int count2=sels7AllOptions.size();
for(int s7=0;s7<count2;s7++) {
drpdwns7.selectByIndex(s7);
catch(StaleElementException e2){

System.out.println("selected value"+s7);
}
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS