Selenium+Java显示等待和隐式等待

  描述:用来操作界面上的等待时间,显示等待是等待某一条件满足,条件满足后进行后面的操作;隐式等待是给出一个等待时间,在时间到达之前若满足条件,则立即执行后续操作。

  

public class TestSelenium {
    
    WebDriver webDriver = null;
    
    @Before
    public void Setup(){
        File chromeDriverPath = new File("D:\Selenium\webdriver\chromedriver.exe");
        System.setProperty("webdriver.chrome.driver", chromeDriverPath.getAbsolutePath());
        webDriver = new ChromeDriver();
    }
  @Test
    public void testWait(){
        webDriver.get("http://www.baidu.com");
        
        WebElement webElement = webDriver.findElement(By.name("wd"));
        webElement.sendKeys("Selenium 2");
        webElement.submit();
        
        //1. 显示等待
        (new WebDriverWait(webDriver, 10)).until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driver) {
                // TODO Auto-generated method stub
                return driver.getTitle().toLowerCase().startsWith("selenium");
            }
            
        });
        
        System.out.println("Page title is:"+webDriver.getTitle());
        
        webDriver.navigate().back();
        
        //2. 隐式等待
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        
        webDriver.findElement(By.xpath("//*[@id="u1"]/a[1]")).click();
        
        webDriver.quit();
    }
}

  

原文地址:https://www.cnblogs.com/yigui/p/7200853.html