I am getting Stale element exception when I try to automate angular js - drop down using Selenium with Java

0 votes

I have around 40 items in my Drop down and I can see it by scrolling down in Gui - But when I inspect the DOM, I can see the code for only 10. I can see the rest only when I scroll it in GUI. (Mean to say- items are loaded in DOM only when I scroll) - I tried to pick the 11th item from the drop down using xpath. So I scrolled it using Java script and then waited for few seconds for items to be loaded and then tried to pick it using xpath. But getting stale element exception.

My solution:

driver.findElement(By.xpath("//input[@role='combobox']")).click();
List<WebElement> options = driver.findElements(By.xpath("//ng-dropdown-panel//div[@role='option']"));
KeywordUtil.logInfo("effectiveDate Size: "+options.size())
for(int i=1;i<options.size();i++) {
       if ( options.get(i).getText().trim().equals("01/01/2016")) {
             KeywordUtil.logInfo("True: "+options.get(i).getText())
              options.get(i).click();
              break;
       }
       else{
              String str=driver.findElement(By.xpath("//ng-dropdown-panel//div[@role='option']["+i+"]")).getAttribute("id")
              KeywordUtil.logInfo("id: "+str)
              String[] a=str.split("-");
              WebElement abc = driver.findElement(By.xpath("//ng-dropdown-panel//div[contains(@id,'-"+Integer.parseInt(a[1])-1+"')]"))
              ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", abc);
              //WebElement abc = driver.findElements(By.xpath("//ng-dropdown-panel"))
//            ((JavascriptExecutor)driver).executeScript("arguments[0].scrollTo(0,0);");
              //((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", options.get(i));
       Thread.sleep(1000);
//            WebElement r=driver.findElement(By.xpath("//ng-dropdown-panel//div[@role='option']"))
//          ((JavascriptExecutor)driver).executeScript("window.scrollBy(0,document.body.scrollHeight)");
//            options = driver.findElements(By.xpath("//ng-dropdown-panel//div[@role='option']"));
//            WebElement scroll = driver.findElement(By.xpath("//ng-dropdown-panel//div[@role='option']"));
//            scroll.sendKeys(Keys.PAGE_UP);
       }

}

Angular js code:

<ng-dropdown-panel class="ng-dropdown-panel ng-star-inserted ng-select-top" id="ac20f159de88" style="opacity: 1;"><!---->
<div class="ng-dropdown-panel-items scroll-host">
<div class="total-padding" style="height: 2288px;"></div>
<div class="scrollable-content" style="transform: translateY(1508px);"><!----><!---->
<div class="ng-option ng-star-inserted" role="option" aria-selected="false" id="ac20f159de88-29"><!----><!----> 02/21/2020 </div>
<div class="ng-option ng-star-inserted" role="option" aria-selected="false" id="ac20f159de88-30"><!----><!----> 02/20/2020 </div>
<div class="ng-option ng-star-inserted" role="option" aria-selected="false" id="ac20f159de88-31"><!----><!----> 02/19/2020 </div>
<div class="ng-option ng-star-inserted" role="option" aria-selected="false" id="ac20f159de88-32"><!----><!----> 02/13/2020 </div>
<div class="ng-option ng-option-selected ng-option-marked ng-star-inserted" role="option" aria-selected="true" id="ac20f159de88-33"><!----><!----> 02/11/2020 </div>
<div class="ng-option ng-star-inserted" role="option" aria-selected="false" id="ac20f159de88-34"><!----><!----> 02/10/2020 </div>
<div class="ng-option ng-star-inserted" role="option" aria-selected="false" id="ac20f159de88-35">
<!----><!----> 02/08/2020 </div>
<div class="ng-option ng-star-inserted" role="option" aria-selected="false" id="ac20f159de88-36"><!----><!----> 02/07/2020 </div>
<div class="ng-option ng-star-inserted" role="option" aria-selected="false" id="ac20f159de88-37"><!----><!----> 02/05/2020 </div>
<div class="ng-option ng-star-inserted" role="option" aria-selected="false" id="ac20f159de88-38"><!----><!----> 02/01/2020 </div>
<div class="ng-option ng-star-inserted" role="option" aria-selected="false" id="ac20f159de88-39"><!----><!----> 01/30/2020 </div>
<div class="ng-option ng-star-inserted" role="option" aria-selected="false" id="ac20f159de88-40"><!----><!----> 01/25/2020 </div>
<div class="ng-option ng-star-inserted" role="option" aria-selected="false" id="ac20f159de88-41"><!----><!----> 01/24/2020 </div><!----><!----><!----><!----></div></div><!----></ng-dropdown-panel>

Feb 12, 2020 in Selenium by dhivya
• 120 points
3,505 views

1 answer to this question.

0 votes

Hey @Dhivya, StaleElementReferenceException is thrown when an object for a particular web element was created in the program without any problem and however; this element is no longer present in the window. This can happen if there was a navigation to another page, DOM has refreshed or A frame or window switch happened.

To avoid issues due to DOM refresh, You could probably use Dynamic Xpath.

Hope it helps!!

To know more about Angular, Join Angular online training today.

Thank you!!

answered Feb 12, 2020 by Sirajul
• 59,230 points
I have used dynamic xpath only to find...
Try to locate your element after you are scrolling the page, so that it isn't affected after the dom refereshes.

Related Questions In Selenium

0 votes
1 answer
0 votes
0 answers

I am getting no such alert exception in selenium when i tried to run the script

I am getting error after executing the ...READ MORE

Feb 4, 2020 in Selenium by puneeth
• 120 points
3,191 views
0 votes
1 answer

Select a drop down value of angular js application using selenium using text

To select drop down use following, driver.findElements(By.className("Your dropdown ...READ MORE

answered Jun 18, 2018 in Selenium by Samarpit
• 5,910 points
11,226 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,690 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,151 views
0 votes
2 answers

What is the role of TestNG & JUnit frameworks in Selenium?

TestNG and JUnit are test frameworks . it ...READ MORE

answered Sep 4, 2020 in Selenium by Sri
• 3,190 points
2,898 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,230 views
+1 vote
1 answer

how to automate google Signup form in selenium using python

Try the following code:  from selenium.webdriver import Chrome, ...READ MORE

answered Mar 23, 2020 in Selenium by Sirajul
• 59,230 points
7,286 views
0 votes
1 answer

Which exception is raised when an element is not found in an HTML DOM using XPath

Hey, A stale element reference exception is thrown in one of two cases If ...READ MORE

answered Jul 30, 2020 in Selenium by Sirajul
• 59,230 points
10,637 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP