0

Below is the code where selenium is unable to find the element.

By machineUIList = By.xpath("//div[@class='thmbinnerdiv']");
        Thread.sleep(5000);

        ArrayList<String> machineUITextList = objUtilities.getElementsTextList(machineUIList);
        Thread.sleep(5000);
        System.out.println(machineUITextList);

So I am trying to get a list of machine names and store it in an arraylist. The HTML DOM is enter image description here

But somehow it fails and doesnt work for me. It always gives me below exception: stale element reference: element is not attached to the page document

Added the code for --getElementsTextList

public ArrayList<String> getElementsTextList(By locator) {
        ArrayList<String> eleTextList = new ArrayList<String>();
        List<WebElement> eleList = waitForVisibilityOfElements(locator, 30);
        for (WebElement e : eleList) {
            if (!e.getText().isEmpty()) {
                eleTextList.add(e.getText());
            }
        }
        return eleTextList;
    }

Code of waitforVisibilityofElement

public List<WebElement> waitForVisibilityOfElements(By locator, int timeOut) {
        WebDriverWait wait = new WebDriverWait(driver, timeOut);
        return wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator));

    }

HTML DOM:

<div _ngcontent-xmt-c28="" class="col-lg-9 col-md-8 col-sm-6 col-xs-12 reducewidthxslandscap"><div _ngcontent-xmt-c28="" class="row"><div _ngcontent-xmt-c28="" ng-class="{'margin-right-15' : linemachinedata.MachineDetailList.length > 6}" class="linemachineblocrightscroll"><div _ngcontent-xmt-c28="" class="col-lg-4 col-md-6 col-sm-12 col-xs-12 machinedetailsinnerwrapper"><div _ngcontent-xmt-c28="" class="thumbnails machineblockdiv machineblockdivheight cursorpointer"><div _ngcontent-xmt-c28="" class="thmbinnerdiv"><h5 _ngcontent-xmt-c28="">TC-34</h5><!--bindings={
  "ng-reflect-ng-if": "false"
}--><span _ngcontent-xmt-c28=""><img _ngcontent-xmt-c28="" src="./assets/Images/stopped1.png"></span><!--bindings={
  "ng-reflect-ng-if": "true"
}--></div><div _ngcontent-xmt-c28="" tabindex="0" ng-reflect-router-link="/Machinedashboard" ng-reflect-query-params="[object Object]"><div _ngcontent-xmt-c28=""><app-circular-progressbar _ngcontent-xmt-c28="" _nghost-xmt-c27="" ng-reflect-percentage="0"><div _ngcontent-xmt-c27="" class="c100 p0 rounded-prgsbar red" ng-reflect-ng-class="[object Object]"><span _ngcontent-xmt-c27="" class="machperasly font-19">0<h6 _ngcontent-xmt-c27="" class="machnameper">.00%</h6></span><div _ngcontent-xmt-c27="" class="slice"><div _ngcontent-xmt-c27="" class="bar"></div><div _ngcontent-xmt-c27="" class="fill"></div></div></div></app-circular-progressbar><div _ngcontent-xmt-c28="" class="thmbinnerdiv1"><label _ngcontent-xmt-c28="">NewPart</label><h6 _ngcontent-xmt-c28="">Part Number</h6></div><div _ngcontent-xmt-c28="" class="thmbinnerdiv2 margin-top-10 margin-bottom-10"><label _ngcontent-xmt-c28="">00</label><h6 _ngcontent-xmt-c28="">Operation Number</h6></div><div _ngcontent-xmt-c28="" class="innerdiv2"><div _ngcontent-xmt-c28="" class="progress_summury"><label _ngcontent-xmt-c28="" class="firstsumry">0</label><label _ngcontent-xmt-c28="" class="secondsumry">0</label><label _ngcontent-xmt-c28="" class="numcount thirdsumry text-right">0</label></div><div _ngcontent-xmt-c28="" class="progress_summury"><h6 _ngcontent-xmt-c28="" class="firstsumry">Parts Produced</h6><h6 _ngcontent-xmt-c28="" class="secondsumry">Parts Rejected</h6><h6 _ngcontent-xmt-c28="" class="thirdsumry text-right">Target</h6></div></div><app-progressbar _ngcontent-xmt-c28="" ng-reflect-percentage="0"><div class="progress progress-line"><div role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" ng-reflect-ng-class="[object Object]" class="red-prg-bar" style="width: 0%;"><span class="sr-only"> 0 % Complete</span></div></div></app-progressbar><div _ngcontent-xmt-c28="" class="thmbinnerdiv3"><h6 _ngcontent-xmt-c28="">Total Downtime</h6></div><div _ngcontent-xmt-c28="" class="thmbinnerdiv5"><span _ngcontent-xmt-c28="" class="glyphicon glyphicon-time margin-right-5 color-red small"></span><label _ngcontent-xmt-c28="" class="margin-left-3">0h 0m </label></div></div><!--bindings={
  "ng-reflect-ng-for-of": "[object Object]"
}--><!--bindings={
  "ng-reflect-ng-if": "false"
}--></div><!--bindings={
  "ng-reflect-ng-for-of": "[object Object]"
}--><!--bindings={
  "ng-reflect-ng-if": "false"
}--></div></div><div _ngcontent-xmt-c28="" class="col-lg-4 col-md-6 col-sm-12 col-xs-12 machinedetailsinnerwrapper"><div _ngcontent-xmt-c28="" class="thumbnails machineblockdiv machineblockdivheight cursorpointer"><div _ngcontent-xmt-c28="" class="thmbinnerdiv"><h5 _ngcontent-xmt-c28="">TC-25</h5><!--bindings={
  "ng-reflect-ng-if": "false"
}--><span _ngcontent-xmt-c28=""><img _ngcontent-xmt-c28="" src="./assets/Images/stopped1.png"></span><!--bindings={
  "ng-reflect-ng-if": "true"
}--></div><div _ngcontent-xmt-c28="" tabindex="0" ng-reflect-router-link="/Machinedashboard" ng-reflect-query-params="[object Object]"><div _ngcontent-xmt-c28=""><app-circular-progressbar _ngcontent-xmt-c28="" _nghost-xmt-c27="" ng-reflect-percentage="0"><div _ngcontent-xmt-c27="" class="c100 p0 rounded-prgsbar red" ng-reflect-ng-class="[object Object]"><span _ngcontent-xmt-c27="" class="machperasly font-19">0<h6 _ngcontent-xmt-c27="" class="machnameper">.00%</h6></span><div _ngcontent-xmt-c27="" class="slice"><div _ngcontent-xmt-c27="" class="bar"></div><div _ngcontent-xmt-c27="" class="fill"></div></div></div></app-circular-progressbar><div _ngcontent-xmt-c28="" class="thmbinnerdiv1"><label _ngcontent-xmt-c28="">NewPart</label><h6 _ngcontent-xmt-c28="">Part Number</h6></div><div _ngcontent-xmt-c28="" class="thmbinnerdiv2 margin-top-10 margin-bottom-10"><label _ngcontent-xmt-c28="">00</label><h6 _ngcontent-xmt-c28="">Operation Number</h6></div><div _ngcontent-xmt-c28="" class="innerdiv2"><div _ngcontent-xmt-c28="" class="progress_summury"><label _ngcontent-xmt-c28="" class="firstsumry">0</label><label _ngcontent-xmt-c28="" class="secondsumry">0</label><label _ngcontent-xmt-c28="" class="numcount thirdsumry text-right">0</label></div><div _ngcontent-xmt-c28="" class="progress_summury"><h6 _ngcontent-xmt-c28="" class="firstsumry">Parts Produced</h6><h6 _ngcontent-xmt-c28="" class="secondsumry">Parts Rejected</h6><h6 _ngcontent-xmt-c28="" class="thirdsumry text-right">Target</h6></div></div><app-progressbar _ngcontent-xmt-c28="" ng-reflect-percentage="0"><div class="progress progress-line"><div role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" ng-reflect-ng-class="[object Object]" class="red-prg-bar" style="width: 0%;"><span class="sr-only"> 0 % Complete</span></div></div></app-progressbar><div _ngcontent-xmt-c28="" class="thmbinnerdiv3"><h6 _ngcontent-xmt-c28="">Total Downtime</h6></div><div _ngcontent-xmt-c28="" class="thmbinnerdiv5"><span _ngcontent-xmt-c28="" class="glyphicon glyphicon-time margin-right-5 color-red small"></span><label _ngcontent-xmt-c28="" class="margin-left-3">0h 0m </label></div></div><!--bindings={
  "ng-reflect-ng-for-of": "[object Object]"
}--><!--bindings={
  "ng-reflect-ng-if": "false"
}--></div><!--bindings={
  "ng-reflect-ng-for-of": "[object Object]"
}--><!--bindings={
  "ng-reflect-ng-if": "false"
}--></div></div><div _ngcontent-xmt-c28="" class="col-lg-4 col-md-6 col-sm-12 col-xs-12 machinedetailsinnerwrapper"><div _ngcontent-xmt-c28="" class="thumbnails machineblockdiv machineblockdivheight cursorpointer"><div _ngcontent-xmt-c28="" class="thmbinnerdiv"><h5 _ngcontent-xmt-c28="">TC-24</h5><!--bindings={
  "ng-reflect-ng-if": "false"
}--><span _ngcontent-xmt-c28=""><img _ngcontent-xmt-c28="" src="./assets/Images/stopped1.png"></span><!--bindings={
  "ng-reflect-ng-if": "true"
}--></div><div _ngcontent-xmt-c28="" tabindex="0" ng-reflect-router-link="/Machinedashboard" ng-reflect-query-params="[object Object]"><div _ngcontent-xmt-c28=""><app-circular-progressbar _ngcontent-xmt-c28="" _nghost-xmt-c27="" ng-reflect-percentage="0"><div _ngcontent-xmt-c27="" class="c100 p0 rounded-prgsbar red" ng-reflect-ng-class="[object Object]"><span _ngcontent-xmt-c27="" class="machperasly font-19">0<h6 _ngcontent-xmt-c27="" class="machnameper">.00%</h6></span><div _ngcontent-xmt-c27="" class="slice"><div _ngcontent-xmt-c27="" class="bar"></div><div _ngcontent-xmt-c27="" class="fill"></div></div></div></app-circular-progressbar><div _ngcontent-xmt-c28="" class="thmbinnerdiv1"><label _ngcontent-xmt-c28="">NewPart</label><h6 _ngcontent-xmt-c28="">Part Number</h6></div><div _ngcontent-xmt-c28="" class="thmbinnerdiv2 margin-top-10 margin-bottom-10"><label _ngcontent-xmt-c28="">00</label><h6 _ngcontent-xmt-c28="">Operation Number</h6></div><div _ngcontent-xmt-c28="" class="innerdiv2"><div _ngcontent-xmt-c28="" class="progress_summury"><label _ngcontent-xmt-c28="" class="firstsumry">0</label><label _ngcontent-xmt-c28="" class="secondsumry">0</label><label _ngcontent-xmt-c28="" class="numcount thirdsumry text-right">0</label></div><div _ngcontent-xmt-c28="" class="progress_summury"><h6 _ngcontent-xmt-c28="" class="firstsumry">Parts Produced</h6><h6 _ngcontent-xmt-c28="" class="secondsumry">Parts Rejected</h6><h6 _ngcontent-xmt-c28="" class="thirdsumry text-right">Target</h6></div></div><app-progressbar _ngcontent-xmt-c28="" ng-reflect-percentage="0"><div class="progress progress-line"><div role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" ng-reflect-ng-class="[object Object]" class="red-prg-bar" style="width: 0%;"><span class="sr-only"> 0 % Complete</span></div></div></app-progressbar><div _ngcontent-xmt-c28="" class="thmbinnerdiv3"><h6 _ngcontent-xmt-c28="">Total Downtime</h6></div><div _ngcontent-xmt-c28="" class="thmbinnerdiv5"><span _ngcontent-xmt-c28="" class="glyphicon glyphicon-time margin-right-5 color-red small"></span><label _ngcontent-xmt-c28="" class="margin-left-3">0h 0m </label></div></div><!--bindings={
  "ng-reflect-ng-for-of": "[object Object]"
}--><!--bindings={
  "ng-reflect-ng-if": "false"
}--></div><!--bindings={
  "ng-reflect-ng-for-of": "[object Object]"
}--><!--bindings={
  "ng-reflect-ng-if": "false"
}--></div></div><!--bindings={
  "ng-reflect-ng-for-of": "[object Object],[object Object"
}--></div></div></div>

Any help is appreciated. Thanks

  • Is that TC-34 machine name ? How many entry do you see in DOM with xpath `//div[@class='thmbinnerdiv']` – cruisepandey May 04 '21 at 10:02
  • Yes, its machine name. 3 machines are listed on the UI with the xpath //div[@class='thmbinnerdiv'] – AutomationTester May 04 '21 at 10:08
  • Share your `HTML` code as text not as image, No one will write that for you to reproduce the case. Also `getElementsTextList()` share the implementation of this details. – Dev May 04 '21 at 10:14
  • first of all, don't use Thread.sleep for selenium tests, use webdriver wait...also, could you, pls, provide the code for the objUtilities.getElementsTextList method? – Marek May 04 '21 at 10:14
  • what does `waitForVisibilityOfElements()` method contains ? – cruisepandey May 04 '21 at 10:24
  • @AutomationTester If 3 machines are listed with that xpath, selenium won't be able to find it, not being unique. Share HTML of the other machines, and some wider HTML of the machine you are targeting. – Mate Mrše May 04 '21 at 10:29
  • Any help on how can i share the HTML part of code? I am new to it. – AutomationTester May 04 '21 at 10:35
  • Open DevTools (press F12 in Chrome or Firefox), find the top-most html tag you need, right click, Copy outer HTML. – Mate Mrše May 04 '21 at 10:50
  • I would like to bring to your notice that the elements on this page like machine names are dynamic and keep changing periodically. I am not sure if this has any affect on the issue i am facing. Though the xpath is always same. – AutomationTester May 04 '21 at 11:24

1 Answers1

0

try with below code :

List<WebElement> machineNames = driver.findElements(by.xpath("//div[@class='thmbinnerdiv']"));
for(WebElement machine : machineNames){
 System.out.println(machine.getText());
}

You need to tweak your xpath bit, use the below to get exact Machine name :

//div[@class='thmbinnerdiv']/h5

to handle stale element exception you may try solution given below :

new WebDriverWait(driver, timeout).ignoring(StaleElementReferenceException.class).until(ExpectedConditions.visibilityOfElement(locator));

in your waitForVisibilityOfElements method.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • This does not work for me and gives me error The method getElementsTextList(By) in the type utilities is not applicable for the arguments (List) – AutomationTester May 04 '21 at 10:28
  • Isn't that obvious ? the return type of `getElementsTextList` method is ArrayList, basically this list `eleTextList`. However I have provided you a generic solution to handle multiple web elements. I have edited my answer for xpath – cruisepandey May 04 '21 at 11:45
  • @AutomationTester : Print `eleTextList.add(e.getText());` inside if block and let me know the console output. – cruisepandey May 04 '21 at 11:50
  • @cruisepandey- It does not got the line machineUITextList = objUtilities.getElementsTextList(machineUIList); and throws a runtime exception under ThrowableClass saying StaleElementReferenceException – AutomationTester May 05 '21 at 05:53