2

I have a dropdown where I am able to select element by index for first time. when i try to select element for second time it is throwing stale element reference error. I tried with try catch block, explicit wait but nothing worked.

WebElement drop = driver.findElement(By.cssSelector("#ctl00_mainPanel_MainPanel1_SearchStop1_DropDownRoute"));

Select sel_drop = new Select(drop);
List<WebElement> drop_count = sel_drop.getOptions();
int drop_size = drop_count.size();
System.out.println("size of drop down" + drop_size);
sel_drop.selectByIndex(1);


JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(250,0)", "");

sel_drop.selectByIndex(10);//this line is causing error-- when I am trying to select element from dropbox for second time.
Pang
  • 9,564
  • 146
  • 81
  • 122
sridevi
  • 23
  • 1
  • 3
  • You state that it fails on the second attempt, but your code only shows one attempt. What happens in between attempts? It's possible that the act of selecting an option alters the DOM, possibly even the select element itself, thereby making the element stale. I'd try re-initializing the select before the second attempt as the options may have changed. – Bill Hileman Mar 02 '18 at 16:32
  • sel_drop.selectByIndex(10); is the second attempt. – sridevi Mar 03 '18 at 17:51
  • Have you confirmed that there is an option with an index of 10 in the select? Does the select re-populate after you select index 1? – Bill Hileman Mar 03 '18 at 21:18
  • Yes, it is present. problem solved. – sridevi Mar 04 '18 at 04:07

2 Answers2

3

StaleElementReferenceException occurs when element you previously found is no longer attached to DOM (HTML Source). It has been changed and it needs to be find again. Element has changed because you performed select operation and it's value has changed.

Solution: Find your element again like this:

WebElement drop = driver.findElement(By.cssSelector("#ctl00_mainPanel_MainPanel1_SearchStop1_DropDownRoute"));

Select sel_drop = new Select(drop);
List<WebElement> drop_count = sel_drop.getOptions();
int drop_size = drop_count.size();
System.out.println("size of drop down" + drop_size);
sel_drop.selectByIndex(1);

//below lines are crucial
drop = driver.findElement(By.cssSelector("#ctl00_mainPanel_MainPanel1_SearchStop1_DropDownRoute"));
sel_drop = new Select(drop);

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(250,0)", "");

sel_drop.selectByIndex(10);//this line is causing error-- when I am trying to select element from dropbox for second time.
Fenio
  • 3,528
  • 1
  • 13
  • 27
  • 1
    Thanks, Rafal. This code of yours works perfectly. I have been trying to find the solution for past couple of days. This really helped. – sridevi Mar 03 '18 at 17:59
0

In absence of the relevant HTML it is tough to guess the reason why you see StaleElementReferenceException while trying to select the second option but at this point it is worth to mention that a valid usecase will contain steps to select only a single element as per the HTML DOM and proceed further with other steps. So as per your code block if selection of the first selectByIndex is working, it is good to go.

WebElement drop = driver.findElement(By.cssSelector("#ctl00_mainPanel_MainPanel1_SearchStop1_DropDownRoute"));
Select sel_drop = new Select(drop);
List<WebElement> drop_count = sel_drop.getOptions();
int drop_size = drop_count.size();
System.out.println("size of drop down" + drop_size);
sel_drop.selectByIndex(1);

Why may the successive select not work

Incase selecting the first <option> calls a JavaScript or AjaxCall the HTML DOM may get changed, new relevant elements may appear on the page and in this case the <select> tag may change position in the DOM Tree. Hence when you try to select the second <option> you need to identify the <select> tag again and then try to select the second <option>

Here you can find a detailed discussion on StaleElementReferenceException

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks, Debanjan.I will definitely check the link. In my Test scenario, there is two drop down and I'm trying to select first, last and middle index combination of both drop down and want to make sure the result is correct. – sridevi Mar 03 '18 at 17:57