0
votes

On clicking the link "Click to load get data via Ajax!" in the below site text will appear, I am trying to print it after it's visible using explicit wait. it is not printing the text

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Explicitwait {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Aditya\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://www.itgeared.com/demo/1506-ajax-loading.html");
        System.out.println(driver.findElement(By.xpath("//*[@id='results']")).isDisplayed());//false
        driver.findElement(By.xpath("//*[@id='content']/a[2]")).click();
        System.out.println("click on the link");
        WebDriverWait d = new WebDriverWait(driver, 10);
        d.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='results']")));
        System.out.println(driver.findElement(By.xpath("//*[@id='results']")).isDisplayed());//true
        System.out.println(driver.findElement(By.xpath("//*[@id='results']")).getText());
    }

}
1
Are you getting any error or the text is not getting printed ? - Sameer Arora
earlier text was not getting printed.....after changing it to visibility from presence it worked and is getting printed - Aditya

1 Answers

3
votes

To get text use visibilityOfElementLocated instead of presenceOfElementLocated:

WebDriverWait d = new WebDriverWait(WebDriverRunner.getWebDriver(), 10);
d.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='results']")));