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

10

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?

Rawden answered 23/5, 2013 at 3:25 Comment(5)
It's working for me: driver.find_element(:id, "551").textImplausible
It's also working for me using xpath: driver.find_element(:xpath, "//a[@id='551']").textImplausible
@TDHM The one which you specified will not work for Java as well as OP.Carilyn
Plenty of answers here, but no discussion of which approach is better than the other. Refer the differences between "innerText", "textContent" etc. from here #21311799 and also medium.com/better-programming/….Demurral
Possible duplicate: How to gettext() of an element in Selenium WebdriverOestradiol
I
32

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");
Inwrap answered 14/7, 2014 at 13:31 Comment(1)
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.Missing
B
18

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

Backman answered 22/3, 2017 at 8:32 Comment(3)
Where does the attribute name "textContent" come from? Is it literal?Oestradiol
That shall be an attribute in the HTML for the field.Gens
[for python devs] I just tried .get_attribute("textContent"), and it is working fine for me too. ThanksSeto
B
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();
Buie answered 23/5, 2013 at 6:50 Comment(2)
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 durationRawden
Hi Everyone, this is the website 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.Rawden
P
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")`.

Proa answered 4/11, 2019 at 18:44 Comment(0)
R
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();
Radiotelephone answered 6/6, 2021 at 14:34 Comment(0)
E
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"));
Etamine answered 23/5, 2013 at 15:52 Comment(2)
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 falseRawden
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.Rawden
C
0

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

Cosgrove answered 31/8, 2016 at 7:51 Comment(0)
I
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.

Invigilate answered 3/2, 2017 at 6:4 Comment(0)
E
0

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

System.out.println(driver.findElement(By.id("modalError")).getAttribute("innerText"));
Enchanter answered 13/6, 2022 at 11:24 Comment(1)
please format the code snippet (as .. well .. code :) to make it readableAdne
A
-1

In a similar situation, this worked for me:

.getAttribute("innerHTML");
.getAttribute("innerText");
.getAttribute("outerText");
Amateur answered 22/12, 2016 at 21:58 Comment(1)
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.Litigable

© 2022 - 2024 — McMap. All rights reserved.