Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to extract the text using gettext in Selenium WebDriver and also unable to click it

I am not able to find the gettext of the below code in the Selenium WebDriver.

<a id="551" class="blueTextNormal1 spc" onclick="sPh(this,'079');return false;" title="079">Country</a>

I want to get the value of Country. I tried using the xpath:

driver.findElement(By.xpath("//*[@id='551']").getText())

but it is not returning any value. When I tried with

driver.findElement(By.xpath("//*[@id='551']")).getAttribute("title"))

I am getting the value as "079".

How can I to proceed?

like image 601
Learner Avatar asked May 23 '13 03:05

Learner


2 Answers

It depends on the code as well. Give a try with the below code:

Instead of getText(), please use getAttribute("innerHTML") which will then return what you are looking for, including any HTML that is invisible.

<div class="no-docs-selected">
    <p class="icon-alert">Please select a doc</p>
</div>

I was looking for Please select a doc, but I didn't succeed with getText(). But the below one worked.

driver.findElement(By.xpath("//p[@class='icon-alert']")).getAttribute("innerHTML");
like image 80
satender Avatar answered Oct 01 '22 02:10

satender


I faced the same issue, and then I updated .getText() to .getAttribute("textContent") and it worked.

like image 40
Ami Desai Avatar answered Oct 01 '22 02:10

Ami Desai