0

Now I want to print all the date like "03/17/2018" in Selenium WebDriver, and here is my HTML inspect in Chrome:

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

Now I can print any single date with its XPath as:

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

But I want to iterator all the date and print all the date, I tried:

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

To get the size of all the children element, but there is an error that I cannot get its size. So my question is how I iterate and print all the dates here? Also, If I want to do iterative jobs in other tables, how can I choose the parent? I mean most time I could get any single data with its XPath in Chrome, but when I want to iterate all the data, I do not know how to choose the parent and get its size. Thank you very much!

4 Answers 4

1

If your requirement is to print all the dates under the heading Encounters and you can create a List of the dates under the heading Encounters and print them using the following code block :

Code Block :

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

Console Output :

03/17/2018
02/17/2018
01/17/2018
11/17/2017
10/17/2016
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks DebanjanB. Now I can print all the dates in the console. But how did you know the parent 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']"? Did you find it in fireBug or Chrome Inspect? Or you wrote it by yourself? I would like to learn the method so I could deal with similar problem in the future. Thanks. When I tried in my Chrome inspect, and fetch the part like parent which is covering all the children. I get xpath "//*[@id="ember3180"]/section"
@YangWang I am afraid, the xpath wasn't found through FireBug or Chrome Inspect. 3180 as in ember3180 looks dynamic to me. So it may not help us much to locate any unique element constantly.
I have accepted you. Here is my another question about selenium in my work : stackoverflow.com/questions/50123421/… Could you look at it and give me some advice? I think I need to learn how to write Xpath by HTML to solve my problems in my work, right?
1

You can take number of elements which has same class, by using findElements and 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);
 }

Here List<E> is List array, which stores all elements from DOM which is containing above span class and Webelement will retrieve text from span class.

3 Comments

Thanks Ishita. Now I can print all the dates in the console. But how did you know the parent xPath "//span[@class='link-text box-margin-Rsm-v2']"? Did you find it in fireBug or Chrome Inspect? Or you wrote it by yourself? I would like to learn the method so I could deal with similar problem in the future. Thanks
@YangWang, From the HTML which you have provided.
@YangWang Please accept this answer with Up vote, so other readers may refer this answer.
1

You can use this code to get all dates:

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

Comments

1

1) Getting a single element:

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

2) Getting a series of elements:

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

The differences are:

  • findElement vs findElements - you need the plural version to get multiple elements.
  • Do not specify the [#] for the span. Saying span[1] will only match the first occurrence, but span will match all the possibilities.

2 Comments

You've got .getText() on the end of both these statements, I don't think that was your intention. List<WebElement> doesn't have a .getText() method.
@anonygoose, good catch. I updated the code to just be WebElements. That will teach me to trust my cut and paste skills without looking

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.