Home > Back-end >  How to get an element within an ArrayList of elements using Selenium Java
How to get an element within an ArrayList of elements using Selenium Java

Time:02-04

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=""  style="height: 396px;"><h4 _ngcontent-llu-c42="">TC - 33</h4><div _ngcontent-llu-c42="" ><div _ngcontent-llu-c42=""><h3 _ngcontent-llu-c42="">53<span _ngcontent-llu-c42="" >.52%</span></h3></div><div _ngcontent-llu-c42=""><h6 _ngcontent-llu-c42="" >OEE</h6></div></div><div _ngcontent-llu-c42=""><div _ngcontent-llu-c42="" ><div _ngcontent-llu-c42="" ><label _ngcontent-llu-c42="" title="Part - 2409">Part - 2409</label><h6 _ngcontent-llu-c42="" >Part Number</h6></div></div><div _ngcontent-llu-c42="" ><div _ngcontent-llu-c42=""><label _ngcontent-llu-c42="">105</label><h1 _ngcontent-llu-c42="" > 196 </h1></div><div _ngcontent-llu-c42=""><h6 _ngcontent-llu-c42="">Parts Produced</h6><h6 _ngcontent-llu-c42="" >Target</h6></div></div><app-progressbar _ngcontent-llu-c42="" _nghost-llu-c41=""><div _ngcontent-llu-c41="" ><div _ngcontent-llu-c41="" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"  style="width: 53.5714%;"><span _ngcontent-llu-c41="" > 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="" ><span _ngcontent-llu-c36=""><span _ngcontent-llu-c36=""  title="TC-33"></span><!----><!----></span><!----></div></app-machine-statusbar></div><!----></div>

Thanks in advance.

CodePudding user response:

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...));
   ...
}
  •  Tags:  
  • Related