-1

I have multiple div classes. I need to get the inner div elements of the text values.

<div data-tab-content-id="2" class="panel active">
  <div class="_1jkaeae">
      <div class="abc-table">
          <div class="aaa nhc">
              <div class="aaa-name">Hello</div>
          </div>
          <div class="aaa mhjss">
              <div class="aaa-name">How</div>
          </div>
      </div>
      <div class="aaa qwqwq">
          <div class="aaa-name">are</div>
      </div>
  </div>
  <div class="aaa kkkk">
      <div class="aaa-name">You</div>
  </div>
</div>
<div class="aaa cccc">
  <div class="aaa-name">doing</div>
</div>
</div>
<div class="aaa cvdsdws">
  <div class="aaa-name">Welcome</div>

</div>
</div>
<div class="aaa klqwq">
  <div class="aaa-name">to the web</div>
</div>
</div>
</div>

From the above html the output I am looking for is to get all the text values (Hello,How,are,you,doing,welcome,to the web).

I would appreciate if someone can help me.

newbie
  • 147
  • 1
  • 1
  • 8

5 Answers5

0

How,are,you,doing,welcome,to the web they all share same class name, you can go ahead with class name, css selector, or xpath .

You can use this css selector :

.aaa-name

Code :

driver.findElement(By.cssSelector(".aaa-name")).getText();

Xpath would be :

//div[@class='aaa-name']

Code :

driver.findElement(By.xpath("//div[@class='aaa-name']")).getText();  

Hope this helps.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

The texts you are looking for belong to the same class name. So you can find the texts using the className method.
You can find it like:

driver.findElement(By.className("aaa-name")).getText();
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20
0

As they all share same class names, You need to get all elements using the class name i.e aaa-name and then loop through elements to get desired output. e.g:

List<WebElement> elements = driver.findElements(By.className("aaa-name"));
for(WebElement element: elements){
    //perform your logic here...
    System.out.println(element.getText());
}
Mustahsan
  • 3,852
  • 1
  • 18
  • 34
0

Lets divide the problems in steps:

  1. Get list of all the div elements for which you need text

    List<WebElement> ele_list = driver.findElements(By.xpath("//div[@class='aaa-name']"))
    
  2. Loop through above list and get the text of each element from above list and print:

    List<String> text_element = new ArrayList<String>()
    for(WebElement ele : ele_list){
        text_element.add(ele.getText());
    }
    

Hope it helps :)

Popeye
  • 11,839
  • 9
  • 58
  • 91
PRERNA PAL
  • 381
  • 2
  • 10
  • thank you @Popeye.your solution worked. but I am facing another issue. After getting the texts I need to assert them. Here is the code I tried. – newbie Jul 30 '19 at 00:50
  • @newbie you can have the expected data saved in a list and can add assert in the for loop itself. for e.g. `Assert.assertEquals(ele.getText(), "");` Please add the code that you are trying for assertion in the question itself. Let us have a look and try to figure out if there are any issues in it. – PRERNA PAL Jul 30 '19 at 05:25
  • @newbie if the above answer works for you, could you mark it as answer by clicking the hollow under vote down arrow please? – PRERNA PAL Jul 31 '19 at 06:57
0

To handle dynamic element Induce WebDriverWait and visibilityOfAllElementsLocatedBy and use the following css selector.

WebDriverWait wait = new WebDriverWait(driver, 30);
List<WebElement> elements = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.aaa-name")));
        for(int i=0;i<elements.size();i++)
        {
            System.out.println(elements.get(i).getText());

        }

Output on console :

Hello
How
are
You
doing
Welcome
to the web
KunduK
  • 32,888
  • 5
  • 17
  • 41