1. 設置顯示等待java
Java代碼:dom
public static WebElement WaitForElement(WebDriver driver, String locator) { WebElement targetElement = (new WebDriverWait(driver,10)).until(ExpectedConditions.presenceOfElementLocated(By.xpath(locator))); return targetElement; }
1 WebDriver driver = new FirefoxDriver(); driver.get("http://somedomain/url_that_delays_loading"); 2 WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
2. 設置隱式等待ide
Java代碼:url
WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://i8xiaoshi.com"); WebElement myDynamicElement = driver.findElement(By.id("xxxx"));
1 WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://somedomain/url_that_delays_loading"); 2 WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));