明确等待硒

时间:2019-02-16 07:05:31

标签: selenium selenium-webdriver webdriverwait

在下面的站点文本中,单击链接“ 单击以通过Ajax加载获取数据!”时,我会尝试使用显式等待将其可见后进行打印。它没有打印文本

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 个答案:

答案 0 :(得分:4)

要获取文字,请使用visibilityOfElementLocated而不是presenceOfElementLocated

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