Appium 如何等待元素加载

不多说,直接上代码:

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

import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2015/1/23.
 */
public class TaquTools {

    /**
     * 等待元素加载
     *
     * @param driver           driver
     * @param by               定位方式
     * @param waitTime         等待时间
     */
    public static void waitForVisible(WebDriver driver, final By by, int waitTime) {
        WebDriverWait wait = new WebDriverWait(driver, waitTime);
        for (int attempt = 0; attempt < waitTime; attempt++) {
            try {
                driver.findElement(by);
                break;
            } catch (Exception e) {
                driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
            }
        }
        wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    }


 
  
使用:

TaquTools.waitForVisible(driver, By.id("tvEntranceOne"), 5);// 等待某元素加载,5S未能加载变为超时

同样这个方法在WebDriver也是可用


你可能感兴趣的:(selenium,2.0,Appium)