0

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);
Andrei Konstantinov
  • 6,971
  • 4
  • 41
  • 57
RK007
  • 23
  • 1
  • 10
  • what is printed? – Moshe Slavin Jun 26 '19 at 10:17
  • For the record: follow java naming conventions. Variable/field names should go camelCase in Java. So that (very hard to read) name CaseManagersreceivingreminders might better be caseManagersReceivingReminders ! – GhostCat Jun 26 '19 at 10:30

5 Answers5

1

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:

enter image description here

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • this worked fine with little change in my xpath. Looks cool now. Can we match this values with the values stored in an array list and how please suggest. – RK007 Jun 26 '19 at 12:41
0

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());
frianH
  • 7,295
  • 6
  • 20
  • 45
0

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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

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);
        }
RK007
  • 23
  • 1
  • 10