简体   繁体   English

如何在硒中将值从列表存储到arraylist

[英]How to store values from a list to arraylist in selenium

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);

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 您可能需要修改XPath表达式以查找包含 fa-user 类的 <i>标记 ,并检索其innerText属性以用于其后继同级

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: 演示:

在此处输入图片说明

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());

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. 当您以“(.//tag//subtag)[index]”格式指定xpath时,它的工作方式更像一个列表,但是相反,“ .// tag // subtag [index]”在以下位置搜索第n个子级家长。

Presumably you are looking to print the values eg 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: 大概您正在寻找打印值,例如Veena Pujari(律师)Ranjit Nayak(授权代表)等,因此您需要为WebViewrAllAllssLocatedBy visibilityOfAllElementsLocatedBy()引入WebDriverWait ,并且可以使用Java8stream()map()以及可以使用以下解决方案:

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 您可以在如何使用Selenium和Java提取表元素的id属性的动态值中找到相关的讨论。

I found the solution, Thanks all! 我找到了解决方案,谢谢大家!

Note: CaseManagersreceivingreminders- is a global arraylist stored 注意:CaseManagersreceivingreminders-是存储的全局数组列表

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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM