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

    System.out.println(rc);

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

        System.out.println(name);

    }

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

May 17, 2018 in Selenium by Martin
• 4,320 points
22,879 views

1 answer to this question.

0 votes

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

//span[@class='v-mid'] 

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,910 points

Related Questions In Selenium

0 votes
3 answers

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

Try using List <WebElement> to access all similar elements ...READ MORE

answered Dec 16, 2020 in Selenium by Roshni
• 10,480 points
87,807 views
0 votes
1 answer
0 votes
2 answers

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

some of the controls not have id ...READ MORE

answered Sep 4, 2020 in Selenium by Sri
• 3,190 points
1,822 views
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
• 58,020 points
912 views
0 votes
2 answers

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,500 points
13,793 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,198 views
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,910 points
7,281 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,909 views
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,910 points
11,293 views
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,910 points
1,543 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP