Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get text from span class in selenium

<th>     
    <span class="firstLanguage">Zeit</span>
</th>
<th>
    <span class="firstLanguage">Nach</span>
</th>
<th>
    <span class="firstLanguage"> </span>
</th>
<th>
    <span class="firstLanguage">Über</span>
</th>
<th>
    <span class="firstLanguage">Gleis</span>
</th>

How can I extract the text from span tags in selenium. Is it via classname, but all four class = "firstLanguage"

like image 947
MBA Avatar asked Feb 13 '23 14:02

MBA


2 Answers

You can try below snippet.

 int count = selenium.getXpathCount("//span[@class='firstLanguage']").intValue();
 for(int i =1 ; i <= count ; i ++){
         System.out.println(selenium.getText("//span["+i+"]"));
 }

This will return you all the span elements defined by the class firstLanguage and you can iterate the list to take text out of them.

like image 74
Shishir Kumar Avatar answered Feb 16 '23 03:02

Shishir Kumar


driver.findElement(By.xpath("/html/body/span")).getText();

will display "Zeit"

driver.findElement(By.xpath("/html/body/span[2]")).getText();
will display "Nach"

likewise


driver.findElement(By.xpath("/html/body/span[5]")).getText();
will display "Gleis"
like image 41
VS Achuthanandan Avatar answered Feb 16 '23 03:02

VS Achuthanandan