Share
Go down
gpmdf2
Posts : 1
Join date : 2012-09-20

How to read text from an element?

on Thu Sep 20, 2012 11:29 pm
Hi, everybody!

I need to read the text from an element. I hav the following:

Code:

  driver = new FirefoxDriver();
  driver.findElement(By.name("myElementID")).getText();

This element HAS text, but the returned text is empty (returns "" ). What is happening? Why I can not get the text?

I can put text inside this element like this:

Code:

  driver.findElement(By.name("myElementID")).sendKeys("someText");

The text is written, works fine. So, why am I having problems when trying to read???
popsha
Amateur
Amateur
Posts : 60
Join date : 2012-04-14

Re: How to read text from an element?

on Mon Sep 24, 2012 9:41 am
Hi,


try like this

System.out.println(driver.findElement(By.name("myElementID")).getText());
qappbc
Amateur
Amateur
Posts : 38
Join date : 2011-10-13

Re: How to read text from an element?

on Mon Oct 29, 2012 4:09 am
are there other identifiers besides 'name' for this element? Like id, class, etc. ?
if yes, then try (using class in example below)

System.out.println(driver.findElements(By.xpath("//*[@class='{whatever the class is}']").getText());
AnuSrini
Posts : 2
Join date : 2012-10-22

Pls help me in clicking on the links

on Tue Oct 30, 2012 11:54 am
How to click on these links?

Can u please let me know? I need to get all the links available in the page and click on all the links. am stuck with clicking on the links.



popsha wrote:Hi,


try like this

System.out.println(driver.findElement(By.name("myElementID")).getText());
popsha
Amateur
Amateur
Posts : 60
Join date : 2012-04-14

Re: How to read text from an element?

on Tue Oct 30, 2012 4:21 pm
Hi AnuSrini,

divide your xpath by rows of a web table. for instance,
xpath = //table[1]/tbody/tr[1]/td[2]/a, here tr[1] is first row number of the wen table.
below is the code for above xpath (web table)

Code:

String xpath_1 = "//table[1]/tbody/tr["
String xpath_2 = "]]/td[2]/a"
try{
int i=1;
while(true){
String val = driver.findElement(By.xpath("xpath_1"+i+"xpath_2")).getText();

i++;
}
} catch(Exception e){}



now you can iterate the "i" to get the link clicked where needed.\

hope this might help you!
ashish_techie
Amateur
Amateur
Posts : 73
Join date : 2011-08-09

Re: How to read text from an element?

on Wed Oct 31, 2012 12:45 am
Is the element hidden?... getText can be used only with visible elements.

Regards
Ashish

For selenium tutorials:
www.qtpselenium.com/selenium-tutorials
Sponsored content

Re: How to read text from an element?

Back to top
Permissions in this forum:
You cannot reply to topics in this forum