使用显式等待和验证 Hello world

Use Explicit wait and verify Hello world

我需要这样做 1. http://the-internet.herokuapp.com/dynamic_loading/1 2.使用显式等待30秒 3. 单击开始按钮并验证 Hello World!。

我写了下面的代码但是element.getText是空的。

    driver.get("http://the-internet.herokuapp.com/dynamic_loading/1");
    WebDriverWait wait=new WebDriverWait(driver, 30);
    WebElement all=driver.findElement(By.xpath("//*[@id='start']/button"));
    all.click();
    WebElement element=wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='finish']/h4")));
    driver.findElement(By.xpath("//*[@id='finish']/h4"));
    String text=element.getText();
    System.out.println("hi:"+text);
    //assertEquals("Hello World!", text);

改变ExpectedConditions.presenceOfElementLocated

ExpectedConditions.visibilityOfElementLocated

当您使用 presenceOfElementLocated 时,它会检查 DOM 以查看是否找到指定的元素,无论它的可见性如何。因此,您会发现文本为空,因为该元素存在于 DOM 中但尚不可见。

另一方面,visibilityOfElementLocated 检查指定的元素是否可用且可见。