0

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.

1 Answers1

1
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...));
   ...
}
pburgr
  • 1,722
  • 1
  • 11
  • 26
  • The above solution is not working because my elements that you have in above solution is ArrayList. As I am mainly concerned with the text of the element. So can you help around it? – AutomationTester Feb 02 '22 at 09:17
  • "This DIV has 22 machines." The DIV is a webelement not just a text. That's why you need to work with `List`. – pburgr Feb 02 '22 at 09:36
  • I tried the above code using the second for loop above. Even though I am getting unique element each time i iterate but the child element taken is always the same for all the elements. I am using xpath to find elements. When I look for the xpath I am passing in child element under Developer tool, it returns me all the child elements and not only one. – AutomationTester Feb 03 '22 at 06:28
  • Can you share URL? – pburgr Feb 03 '22 at 06:39
  • Its an internal product of my organization,so I cannot share the site URL. Any other way to analyze whats wrong? – AutomationTester Feb 03 '22 at 06:42
  • Yes, finding another page with same use case. Maybe this one https://demoqa.com/accordian? – pburgr Feb 03 '22 at 06:45
  • Below is the outer HTML – AutomationTester Feb 03 '22 at 09:28
  • Unfortunately this is not useful, as html it's a dry text page. – pburgr Feb 03 '22 at 09:44
  • I have added the outer HTML. There is a card type entity on the site with class = machineblockdiv cursorpointer machinblockheight. Within this class there are various other elements like title= "Part- 2409". So from parent div I want to extract the part number DIV. – AutomationTester Feb 03 '22 at 12:25
  • The solution you provided worked fine. The parent div iterates through all the elements but the child element is not getting iterated as per the parent div. Its always same as of the first parent element. I believe it must be some issue with the xpath i am passing either in parent or child element. – AutomationTester Feb 03 '22 at 12:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241687/discussion-between-automationtester-and-pburgr). – AutomationTester Feb 03 '22 at 12:39
  • I have just tweaked some xpath related changes to find the parent and child DIV. Above solution works fine. – AutomationTester Feb 03 '22 at 15:16