Getting a text of elements in div tag having same class name

0 votes

There are 38 div tags in my web application and I want to get the text present in each div tags.following is the code:-

<div class="w-100 dt pa2 f7"> //this is parent div class

<div class="bg-white-10">//sub class

<div class="dib bg-dirty-green w-100">//sublcass

<p class="pa2 ma0 fl f6"> // class from where i want to capture the text

<span class="v-mid">Mega Contest - 20</span> </p> //this is the text which i want to capture.

I tried the following code: -

WebElement wb = driver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[3]/div[3]")); //CONTEST_TABLE

List<WebElement> list = driver.findElements(By.xpath("//*[@class='pa2 ma0 fl f6']")); //NUMBER_OF_CONTEST

    int rc=list.size();


    for(int i=0;i<rc;i++)


        WebElement Contest_Name= list.get(i).findElement(By.xpath("//*[@class='pa2 ma0 fl f6']"));

        String name = Contest_Name.getText();



I am getting "Mega Contest - 20" 38 times...

May 17, 2018 in Selenium by Martin
• 4,330 points

1 answer to this question.

0 votes

Basically,there is no need to retrieve element in List Array, You can get it single time:


if span class has Unique name,

If its common class name you can also use the following parameter of xpath condition:

 //p[@class='pa2 ma0 fl f6']//following::span[@class='v-mid']
answered May 17, 2018 by Samarpit
• 5,890 points

Related Questions In Selenium

0 votes
1 answer

How to print text from a list of all web elements with same class name in Selenium?

@Rajni, following test case is somewhat similar ...READ MORE

answered Jun 20, 2019 in Selenium by Avantika
0 votes
1 answer
0 votes
1 answer

what is the need of xpath when you have attributes like id ,class,name in selenium?

It is not necessary that every element ...READ MORE

answered Jun 1, 2018 in Selenium by Vinita
• 220 points
0 votes
1 answer

Get text of a webelement in selenium

You can use the following statement to ...READ MORE

answered Feb 13, 2019 in Selenium by Priyaj
• 57,530 points
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,890 points
0 votes
1 answer

Select a drop down value of angular js application using selenium using text

To select drop down use following, driver.findElements(By.className("Your dropdown ...READ MORE

answered Jun 18, 2018 in Selenium by Samarpit
• 5,890 points
0 votes
1 answer

Exclude text from `<script>` tag within the HTML DOM when calling `element.getText()`

As you have already identified the WebElement as element next you ...READ MORE

answered May 8, 2018 in Selenium by Samarpit
• 5,890 points