简体   繁体   中英

How to get a text of elements in div tag having same class name

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

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,

OR if its common class name you can use with following parameter of xpath condition: //p[@class='pa2 ma0 fl f6']//following::span[@class='v-mid']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM