Reputation: 25
I am trying to store the values from a list to a global array for later use/verify in other pages.
My element looks like below:
Tried array list which gives only the 1st line, but I have multiple items in the list.
<div class="col-lg-7">
<div class="clearfix" style="padding-bottom:5px;">
<i class="fa fa-user"></i><span style="margin-left:2%">Veena Pujari (Attorney)</span>
</div>
<div class="clearfix" style="padding-bottom:5px;">
<i class="fa fa-user"></i><span style="margin-left:2%">Ranjit Nayak (Accredited Representative)</span>
</div>
</div>
I am trying to pull the values ex:
List<WebElement> PMPageCMList = driver.findElements(By.xpath("//*[@id='collapseCM']/div[2]/div[2]"));
int totalcms = PMPageCMList.size();
for(int i=1;i<=totalcms;i++){
CaseManagersreceivingreminders.add(driver.findElement(By.xpath("//*[@id='collapseCM']/div[2]/div[2]/div"+"["+i+"]"+"/span")).getText());
System.out.println(CaseManagersreceivingreminders);
Upvotes: 0
Views: 4935
Reputation: 76
Try this instead
CaseManagersreceivingreminders.add(driver.findElement(By.xpath("(.//*[@id='collapseCM']/div[2]/div[2]/div/span)["+i+"]")).getText());
When you specify xpath in "(.//tag//subtag)[index]" format, it works more like a list But on the contrary, ".//tag//subtag[index]" searches for the nth child under a parent.
Upvotes: 0
Reputation: 193208
Presumably you are looking to print the values e.g. Veena Pujari (Attorney), Ranjit Nayak (Accredited Representative), etc, so you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy()
and you can use Java8's stream()
and map()
and can use the following solution:
List<String> myValues = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class='clearfix']/i[@class='fa fa-user']//following::span[1]"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList());
System.out.println(myValues);
You can find a relevant discussion in How to extract the dynamic values of the id attributes of the table elements using Selenium and Java
Upvotes: 0
Reputation: 25
I found the solution, Thanks all!
Note: CaseManagersreceivingreminders- is a global arraylist stored
List<WebElement> PMPageCMList = driver.findElements(By.xpath("//*[@id='collapseCM']/div[2]/div[2]/div"));
int totalcms = PMPageCMList.size();
for(int i=1;i<=totalcms;i++){
CaseManagersreceivingreminders.add(driver.findElement(By.xpath("//*[@id='collapseCM']/div[2]/div[2]/div"+"["+i+"]"+"/span")).getText());
System.out.println(CaseManagersreceivingreminders);
}
Upvotes: 0
Reputation: 168122
If you're interested in the following values:
Veena Pujari (Attorney)
Ranjit Nayak (Accredited Representative)
you might want to amend your XPath expression to look for <i>
tag which contains fa-user
class and retrieve its innerText property for its following-sibling
List<String> PMPageCMList = driver.findElements(By.xpath("//i[contains(@class,'fa-user')]/following-sibling::span"))
.stream()
.map(user -> user.getAttribute("innerText"))
.collect(Collectors.toList());
PMPageCMList.forEach(System.out::println);
Demo:
Upvotes: 1
Reputation: 7563
Try changing this line :
CaseManagersreceivingreminders.add(driver.findElement(By.xpath("//*[@id='collapseCM']/div[2]/div[2]/div"+"["+i+"]"+"/span")).getText());
To be :
CaseManagersreceivingreminders.add(driver.findElement(By.xpath("(//*[@id='collapseCM']/div[2]/div[2])[" +i +"]")).getText());
Upvotes: 0