GetText not working | Selenium Forum
M
Posted on 28/04/2016
.getText() is not returning anything for any of the elements on the webpage. Do you know whats d problem?

M
Replied on 28/04/2016

there must be no text in a given xpath to the website.


M
Replied on 28/04/2016

This sample code is also not working. I have jdk 1.7. Is it jdk issue? I guess you said jdk1.8 may create problem with getText()?

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Yahoo {

/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// text , properties, xls

//System.setProperty("webdriver.chrome.driver", "H:\\chromedriver.exe");
//ChromeDriver driver = new ChromeDriver();
//FirefoxDriver driver = new FirefoxDriver();

// HW
String browser = "Mozilla"; // properties
WebDriver driver = null;

if(browser.equals("Mozilla"))
driver = new FirefoxDriver();
else if(browser.equals("Chrome")){
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\chromedriver.exe");
driver = new ChromeDriver();
}else if(browser.equals("IE")){
// exe path
}


driver.get("http://yahoo.com");
System.out.println("test:"+driver.findElement(By.xpath("//*[@id='UHSearchWeb']")).getText());
// implicit and explicit
Thread.sleep(4000);
String text = driver.findElement(By.xpath("//*[starts-with(@id,'yui_3_4_0_1_136256')]/ul/li[1]/a")).getText();
System.out.println(text);

// interfacename = new calssImple();
}

}


M
Replied on 28/04/2016

is gettext giving you any exception?


M
Replied on 29/04/2016

there is no exception. when im printing the getText on console. its printing nothing.
im using selenium-java-2.53.0.jar
Can you try the above code in your system?
or is getText is working for you?
if its working for you which jar you are using


M
Replied on 29/04/2016

[quote:xopf2gk0]//*[starts-with(@id,'yui_3_4_0_1_136256')]/ul/li[1]/a[/quote:xopf2gk0]


this xpath is not working problem is not with selenium or jar files but with code?


M
Replied on 29/04/2016

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();

driver.get("http://yahoo.com");
System.out.println("test:"+driver.findElement(By.xpath("//*[@id='UHSearchWeb']")).getText());

}

here is my code. can you please execute it and see if its working for you? I tried .getText() will others site too but for that also its not working.


M
Replied on 29/04/2016

try this example of gettext on gmail. yahoo is too dynamic website.



driver.get("http://gmail.com");
System.out
.println("test:" + driver.findElement(By.tagName("h1")).getText());