Search code examples
javaselenium-webdriverfindelement

How to get an element within an ArrayList of elements using Selenium Java


I have an array list in Java that gives me a list of machine names as below:

By mNameUIList = By.xpath("//div[@class='machineblockdiv cursorpointer machinblockheight']//h4");
ArrayList<String> mUITextList = objUtilities.getElementsTextList(mNameUIList );
System.out.println(mUITextList );

This DIV has 22 machines. Each machine has its own individual entities like machine name, part number etc. I want to loop through each machine card and get the value of its individual entity.

for (int i = 0; i < mUITextList .size(); i++) {
                
mUITextList.get(i)

In above code I am unable to pass something like below

mUITextList.get(i).findElement(xpath of individual machine entity)

How do I get the text/value of each entity by parsing through individual machine?

Outer HTML

<div _ngcontent-llu-c42="" class="machineblockdiv cursorpointer machinblockheight" style="height: 396px;"><h4 _ngcontent-llu-c42="">TC - 33</h4><div _ngcontent-llu-c42="" class="innerdiv1"><div _ngcontent-llu-c42=""><h3 _ngcontent-llu-c42="">53<span _ngcontent-llu-c42="" class="percentsign1 m-0">.52%</span></h3></div><div _ngcontent-llu-c42=""><h6 _ngcontent-llu-c42="" class="m-0">OEE</h6></div></div><div _ngcontent-llu-c42=""><div _ngcontent-llu-c42="" class="innerdiv1"><div _ngcontent-llu-c42="" class="mt-5"><label _ngcontent-llu-c42="" title="Part - 2409">Part - 2409</label><h6 _ngcontent-llu-c42="" class="m-0">Part Number</h6></div></div><div _ngcontent-llu-c42="" class="innerdiv2"><div _ngcontent-llu-c42=""><label _ngcontent-llu-c42="">105</label><h1 _ngcontent-llu-c42="" class="pull-right font-small"> 196 </h1></div><div _ngcontent-llu-c42=""><h6 _ngcontent-llu-c42="">Parts Produced</h6><h6 _ngcontent-llu-c42="" class="pull-right">Target</h6></div></div><app-progressbar _ngcontent-llu-c42="" _nghost-llu-c41=""><div _ngcontent-llu-c41="" class="progress progress-line"><div _ngcontent-llu-c41="" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" class="progress-bar green-prg-bar" style="width: 53.5714%;"><span _ngcontent-llu-c41="" class="sr-only"> 53.57142857142857 % Complete</span></div></div></app-progressbar></div><!----><!----><div _ngcontent-llu-c42=""><h5 _ngcontent-llu-c42="">Machine(s)  might needs some attention</h5><app-machine-statusbar _ngcontent-llu-c42="" _nghost-llu-c36=""><div _ngcontent-llu-c36="" class="indicatorwrapper"><span _ngcontent-llu-c36=""><span _ngcontent-llu-c36="" class="machine-indicators normal" title="TC-33"></span><!----><!----></span><!----></div></app-machine-statusbar></div><!----></div>

Thanks in advance.


Solution

  • List<WebElement> elements = driver = findElements(By.xpath("//div[@class='machineblockdiv cursorpointer machinblockheight']//h4"));
    
    for (WebElement element: elements) {
       WebElements childElement = element.findElement(By...));
       ...
    }
    

    or

    for (int i = 0; i < elements.size(); i++) {
       WebElement element = elements.get(i);
       WebElements childElement = element.findElement(By...));
       ...
    }