如何使用Selenium在Java中创建列表然后迭代以打印数据

时间:2018-04-25 22:23:22

标签: java selenium selenium-webdriver webdriver selenium-chromedriver

现在我要打印所有日期,例如" 03/17/2018"在Selenium WebDriver中,这是我在Chrome中的HTML检查:

<div id="ember3175" class="ember-view encounter-list"><header class="box-margin-Bsm"><h3 class="header3a">Encounters</h3></header>
<section class="d-complex-list-container box-padding-Tsm-v2 type-v2">
                <div class="item box-padding-TBxs-v2">
                    <span class="link-text box-margin-Rsm-v2">03/17/2018</span>
                    <span>Encounter (Patient Phone Message)</span>
<!---->                    <div class="chief-complaint">
                        <span class="p-666">CC:</span>
                            <span>Pt. called to follow-up on monospot results.</span>
                    </div>
                </div>
                <div class="item box-padding-TBxs-v2">
                    <span class="link-text box-margin-Rsm-v2">02/17/2018</span>
                    <span>Encounter (SOAP Note)</span>
<!---->                    <div class="chief-complaint">
                        <span class="p-666">CC:</span>
                            <span>chronic sinusitis</span>
                    </div>
                </div>
                <div class="item box-padding-TBxs-v2">
                    <span class="link-text box-margin-Rsm-v2">01/17/2018</span>
                    <span>Encounter (SOAP Note)</span>
<!---->                    <div class="chief-complaint">
                        <span class="p-666">CC:</span>
                            <span>nasal congestion</span>
                    </div>
                </div>
                <div class="item box-padding-TBxs-v2">
                    <span class="link-text box-margin-Rsm-v2">11/17/2017</span>
                    <span>Encounter (SOAP Note)</span>
<!---->                    <div class="chief-complaint">
                        <span class="p-666">CC:</span>
                            <span>nasal congestion</span>
                    </div>
                </div>
                <div class="item box-padding-TBxs-v2">
                    <span class="link-text box-margin-Rsm-v2">10/17/2016</span>
                    <span>Encounter (SOAP Note)</span>
                        <span class="link-text icon-lock"></span>
                    <div class="chief-complaint">
                        <span class="p-666">CC:</span>
                            <span>nasal congestion</span>
                    </div>
                </div>
<!----></section>
</div>

现在我可以用XPath打印任何单个日期:

String time = driver.findElement(By.xpath("//*[@id=\"ember3175\"]/section/div[2]/span[1]")).getText();
System.out.println(time);

但我希望迭代所有日期并打印所有日期,我试过:

int size = driver.findElement(By.xpath("[@id=\"ember3175\"]/section/div/span[1]")).size();

要获取所有子元素的大小,但是有一个错误,我无法得到它的大小。所以我的问题是如何迭代并打印所有日期?另外,如果我想在其他表中执行迭代作业,我该如何选择父作业?我的意思是大多数时候我可以在Chrome中使用XPath获取任何单个数据,但是当我想迭代所有数据时,我不知道如何选择父级并获得其大小。非常感谢你!

4 个答案:

答案 0 :(得分:1)

1)获得单个元素:

WebElement time = driver.findElement(By.xpath("//*[@id=\"ember3175\"]/section/div[2]/span[1]"));

2)获得一系列元素:

List<WebElement> time2 = driver.findElements(By.xpath("//*[@id=\"ember3175\"]/section/div[2]/span"));

区别在于:

  • findElement vs findElements - 您需要复数版才能获得多个元素。
  • 不指定范围的[#]。说span[1]只会匹配第一次出现,但span会匹配所有可能性。

答案 1 :(得分:1)

您可以使用findElements和ListElements获取具有相同类的元素数量:

 List<WebElement> findValue = driver.findElements(By.xpath("//span[@class='link-text box-margin-Rsm-v2']"));

 for (WebElement webElement : findValue) {
 String printValue = webElement.getText();
 System.out.println(printGroupName);
 }

此处List<E>List array,其中存储了DOM中包含上述span类的所有元素,Webelement将从span类中检索文本。

答案 2 :(得分:1)

您可以使用此代码获取所有日期:

public List<WebElement> allDates(){  

List<WebElement> allDates = driver.findElements(By.cssSelector("div[class$='link-text box-margin-Rsm']"));  

for(WebElement elements : allDates){  

System.out.println(elements.getText());

}  
    return allDates;  
}

答案 3 :(得分:1)

如果您的要求是打印遭遇标题下的所有日期,您可以创建日期<的列表 / strong>标题遭遇,并使用以下代码块打印它们:

代码块:

List<WebElement> encounterDates = driver.findElements(By.xpath("//h3[@class='header3a' and contains(.,'Encounters')]//following::section[@class='d-complex-list-container box-padding-Tsm-v2 type-v2']//span[@class='link-text box-margin-Rsm-v2']"));
for (WebElement ele : encounterDates) 
    System.out.println(ele.getAttribute("innerHTML"));

控制台输出:

03/17/2018
02/17/2018
01/17/2018
11/17/2017
10/17/2016