Selenium.getText for hidden element


#1

Hi guys,

first of all forgive my ignorance, I’ve only been working with automation a few days and I’m a total noob. I probably have the wording wrong too.

I’m trying to get the value of a hidden span element.

I first use “waitForElementPresent” so I know it’s there.
then I use:

String tag = selenium.getText (‘css=span.eg’)

but all I get is "null’ instead of the text inside.

If I simply put

click selenium.click (‘css=span.eg’), it works just fine. The link works just fine

One thing that maybe the problem is that on some pages there are several instances of this “span”. Could that be the problem?

Thanks and sorry for this


#2

.getText() (note there’s nothing inside the brackets) should get the innerHTML property, but if your element doesn’t have any innerHTML, but instead has text written (e.g. it is an input element), .getAttribute('value') should get you what you need.

Anyway, this is just some guesswork, if I didn’t hit anything, follow the advice given here:


#3

Hi @anastasios_oik

From the behavior you describe I suspect that the span actually doesn’t contain any text. Can you provide a screenshot of the HTML page ? You can obtain it by right click on the span -> Inspect.

Regards !


#4

Thanks for your suggestios.

I got it using

String tag = WebUI.getAttribute(findTestObject(‘Untitled test case/span.eg’), ‘innerText’) and in the object repsitory I used “css selector” only.

The same also worked with Selenium:

WebElement element= driver.findElement(By.cssSelector()) ;
String val=element.getAttribute(“innerText”)