Reputation: 23
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.
Upvotes: 2
Views: 9919
Reputation: 193338
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);
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
Upvotes: 0
Reputation: 3635
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.
Upvotes: 3