14

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Learner
  • 945
  • 11
  • 23
  • 38
  • It's working for me: driver.find_element(:id, "551").text – Alpha May 23 '13 at 10:46
  • It's also working for me using xpath: driver.find_element(:xpath, "//a[@id='551']").text – Alpha May 23 '13 at 10:49
  • @TDHM The one which you specified will not work for Java as well as OP. – Vignesh Paramasivam Aug 12 '14 at 15:10
  • Plenty of answers here, but no discussion of which approach is better than the other. Refer the differences between "innerText", "textContent" etc. from here https://stackoverflow.com/questions/21311299/nodevalue-vs-innerhtml-and-textcontent-how-to-choose and also https://medium.com/better-programming/whats-best-innertext-vs-innerhtml-vs-textcontent-903ebc43a3fc. – MasterJoe Mar 17 '20 at 05:26
  • Possible duplicate: *[How to gettext() of an element in Selenium Webdriver](https://stackoverflow.com/questions/22087952/how-to-gettext-of-an-element-in-selenium-webdriver)* – Peter Mortensen Nov 22 '20 at 07:42

10 Answers10

31

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");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
satender
  • 1,199
  • 13
  • 12
  • hi @joe I am new to selenium, whatever you have suggested has worked for me but i don't have any clarification and i.e. when to go for getAttribute("atrb") and when to go for getText()? I tried to search for the reason but couldn't find any concrete results. – Abhishek May 23 '20 at 05:40
18

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ami Desai
  • 183
  • 1
  • 6
1

It is really surprising you are able to get attribute title, but not text.

Try with

driver.findelement(By.xpath("//a[@id='551' and contains(text(),'Country')]")).isDisplayed();

or

driver.findelement(By.xpath("//a[@id='551' and text()='Country']")).isDisplayed();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arun
  • 875
  • 3
  • 10
  • 17
  • Hi Arun, Thanks for Replying [link](driver.findElement(By.xpath("//a[@id='551' and contains(text(),'Country')]")).isDisplayed());) iam getting False I Think because of onclick="sPh(this,'079') we are getting as "false" and if iam trying driver.findElement(By.xpath("//a[@id='551']")).click() getting error. org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration – Learner May 23 '13 at 15:46
  • Hi Everyone, this is the website http://www.redbus.in/ here i want to take the phone numbers of each city. The phone numbers will come after when we click on each city . eg. Ahmedabad,Bangalore etc.. Hopes so this helps to understand the application. – Learner May 24 '13 at 05:01
1

I also had a case where getAttribute("innerHTML") worked, but not getText(). It turned out that the element was present, but it was not visible or displayed.

When I scrolled to the element first before calling getText() it worked.

    if(!element.isDisplayed()) 
        scrollIntoView(element);
    element.getText();

The scrollIntoView function is like this:

    public void scrollIntoView(WebElement element) {
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
    }

I therefore don't need to use getAttribute("innerHTML")`.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ldiary
  • 494
  • 2
  • 8
1

This worked for me:

System.out.println(driver.findElement(By.id("error")).getAttribute("textContent"));

Code:

driver.get("https://login.salesforce.com/");
driver.findElement(By.id("username")).sendKeys("Hello");
driver.findElement(By.id("password")).sendKeys("Hello");
driver.findElement(By.id("Login")).click();
System.out.println(driver.findElement(By.id("error")).getAttribute("textContent"));
driver.close();
Matt
  • 12,848
  • 2
  • 31
  • 53
usha sree
  • 13
  • 2
0

Unless it is a typo while copying to Stack Overflow, you are missing a parentheses before getText. Change driver.findElement(By.xpath("//*[@id='551']").getText()) to driver.findElement(By.xpath("//*[@id='551']")).getText()

I tried the following code, and it worked perfectly for me.

WebDriver driver = new ChromeDriver();
driver.get("C:\\test.html");
System.out.println(driver.findElement(By.xpath("//*[@id='551']")).getText());
System.out.println(driver.findElement(By.xpath("//*@id='551']")).getAttribute("title"));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Buddha
  • 4,339
  • 2
  • 27
  • 51
  • Thanks @buddha, i have tested, only the getattribute is returning the value as 079 but not the getText(); i have tested using By.xpath("//a[@id='551' and text()='Country']")).isDisplayed();it is returning false – Learner May 23 '13 at 16:04
  • 1
    Finally i was able to solve the issue the element was hidden so i used the `WebElement labelNode = driver.findElement(By.xpath("//*[@id='551']")); String labelNodeText = (String) ((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML",labelNode); ` was able to return the required ahmedabad. Thanks to everyone who helped me. – Learner May 25 '13 at 09:20
0

Use <WebElement>.getAttribute("value") to extract the text.

Matt
  • 12,848
  • 2
  • 31
  • 53
0
String text1 = driver.findElementByXPath("//input[@value='EFASTGTC']/../ancestor::tr[1]/scipt[1]").getAttribute("innerText"); 
        
System.out.println("Value=" + text1);

This code will work if you want text written between script tag.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

This worked for me. I used getAttribute("innerText") instead of getText().

System.out.println(driver.findElement(By.id("modalError")).getAttribute("innerText"));
Jesse
  • 1,386
  • 3
  • 9
  • 23
-1

In a similar situation, this worked for me:

.getAttribute("innerHTML");
.getAttribute("innerText");
.getAttribute("outerText");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • This is hardly an answer. On what do you call these methods? A WebElement? If so, how do you find that? Also note that an attribute is different from an element's text content. – Robert Dec 22 '16 at 22:06