Java + Selenium element waiting tool class

When we usually use the selenium crawler, we usually encounter the login operation. However, when the goose logs in, if the response time of the other party’s server is too long, or too fast, the time is unstable, then the login verification is performed at this time, and the password is correct. When the information is filled in correctly, it will be very troublesome.

After I checked a lot of information, I still couldn't find a satisfactory element waiting method, so I simply implemented one by myself and shared it with you here.

Function description

function Description
waitElement () Waiting for the specified element. Pass in the WebDriver object and the By object of selenium. If the object exists, execute the callback, or throw an exception
waitUrl() Waiting for the specified page. Pass in the WebDriver object and the web page url. If the object exists, execute the callback, or throw an exception
waitElementText() Wait for the text of the specified element. Pass in the WebDriver object, the By object of selenium, and the text to be judged. If the text exists, execute the callback, or throw an exception

Source code

Main category

/**
 * @Author: KL-Skeleton
 * @Description: ${Description}
 * @Date: Created in 19:22 2020/8/27
 */
public class ElementTimeOutUtils {
    
    
//线程的超时时间默认为一分钟
    private static long runnable_timeout = 60*1000;
    /**
     *线程池
     */
    private static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

    //抛出错误的语句
    private  static  String exceptionStr(WebDriver driver,Object by){
    
    
        return "the " + driver + " time out waiting for the element(" + by+")";
    }

    //元素等待
    public static void waitElement(WebDriver driver, By by, long time) throws ElementTimeOutException {
    
    

        long beginTime = System.currentTimeMillis();
        //如果当前时间减去开始的时间,小于time,并且传入的等待条件不符合,那么将一直堵塞此进程,直到超时,或者条件符合即可。
        while ((System.currentTimeMillis()-beginTime<time)&&(driver.findElements(by).size()==0)){
    
    }
        if(System.currentTimeMillis()-beginTime>=time){
    
    
            throw new ElementTimeOutException(exceptionStr(driver,by));
        }

    }

    public static void waitElement(WebDriver driver, By by,MyRunnable runnable){
    
    
        cachedThreadPool.execute(()->{
    
    
            long beginTime = System.currentTimeMillis();
            //如果当前时间减去开始的时间,小于time,并且传入的等待条件不符合,那么将一直堵塞此进程,直到超时,或者条件符合即可。
            while ((System.currentTimeMillis()-beginTime<runnable_timeout)&&(driver.findElements(by).size()==0)){
    
    }
            if(System.currentTimeMillis()-beginTime<runnable_timeout){
    
    
                runnable.run();
            }else  {
    
    
                runnable.timeout();
            }
        });

    }


    //网页等待
    public static void waitUrl(WebDriver driver,String url,long time) throws ElementTimeOutException{
    
    
        long beginTime = System.currentTimeMillis();
        //如果当前时间减去开始的时间,小于time,并且传入的等待条件不符合,那么将一直堵塞此进程,直到超时,或者条件符合即可。
        while ((System.currentTimeMillis()-beginTime<time)&&(!driver.getCurrentUrl().contains(url))){
    
    }
        if(System.currentTimeMillis()-beginTime>=time){
    
    
            throw new ElementTimeOutException(exceptionStr(driver,url));
        }
    }


    public static void waitUrl(WebDriver driver, String url, MyRunnable runnable){
    
    
        cachedThreadPool.execute(()->{
    
    
            long beginTime = System.currentTimeMillis();
            //如果当前时间减去开始的时间,小于time,并且传入的等待条件不符合,那么将一直堵塞此进程,直到超时,或者条件符合即可。
            while ((System.currentTimeMillis()-beginTime<runnable_timeout)&&(!driver.getCurrentUrl().contains(url))){
    
    }
            if(System.currentTimeMillis()-beginTime<runnable_timeout) {
    
    
                runnable.run();
            }else {
    
    
                runnable.timeout();
            }
        });
    }

    //文本等待
    public static void waitElementText(WebDriver driver, By by, String text, long time) throws ElementTimeOutException{
    
    
        long beginTime = System.currentTimeMillis();
        //如果当前时间减去开始的时间,大于time,并且传入的布尔值为false。则抛出异常。
        while ((System.currentTimeMillis()-beginTime<time)&&(!driver.findElements(by).get(0).getText().contains(text))){
    
    }
        if(System.currentTimeMillis()-beginTime>=time){
    
    
            throw new ElementTimeOutException(exceptionStr(driver,by+" and text:"+text));
        }
    }


    public static void waitElementText(WebDriver driver,  By by,String text,MyRunnable runnable){
    
    
        cachedThreadPool.execute(()->{
    
    
            long beginTime = System.currentTimeMillis();
            //如果当前时间减去开始的时间,小于time,并且传入的等待条件不符合,那么将一直堵塞此进程,直到超时,或者条件符合即可。
            while ((System.currentTimeMillis()-beginTime<runnable_timeout)&&(!driver.findElements(by).get(0).getText().contains(text))){
    
    }
            if(System.currentTimeMillis()-beginTime<runnable_timeout){
    
    
                runnable.run();
            } else  {
    
    
                runnable.timeout();
            }
        });
    }

    //geter and seter
    public static long getRunnable_timeout() {
    
    
        return runnable_timeout;
    }

    public static void setRunnable_timeout(long runnable_timeout) {
    
    
        ElementTimeOutUtils.runnable_timeout = runnable_timeout;
    }

    public abstract static class MyRunnable{
    
    
        public void run(){
    
    }
        public void timeout(){
    
    }
    }





}

Exception class

public   class ElementTimeOutException extends  Exception{
    
    
    public ElementTimeOutException() {
    
    
        super();
    }

    public ElementTimeOutException(String message) {
    
    
        super(message);
    }
}

Usage example

Baidu simulates search, enter "a" in the search box, and then click search until the bottom element "content_bottom" is loaded, then the callback can be realized. If the search is slow for more than 1 minute due to network reasons, or the element "content_bottom" is not found, then Execute the timeout callback.

public static void main(String[] args) {
    
    

        System.setProperty("webdriver.chrome.driver","C:\\Users\\ASUS\\Desktop\\chromedriver7.9.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com/");
        driver.findElement(By.id("kw")).sendKeys("a");
        driver.findElement(By.id("su")).click();
        //方法一:开启元素等待线程,最大等待时间默认为1分钟,这里可以设置为10秒,
        //优点:不会阻塞进程
        ElementTimeOutUtils.setRunnable_timeout(10*1000);
        ElementTimeOutUtils.waitElement(driver, By.id("content_bottom"), new ElementTimeOutUtils.MyRunnable() {
    
    
            @Override
            public void run() {
    
    
                System.out.println("百度搜索加载成功");
            }
            @Override
            public void timeout() {
    
    
                super.timeout();
                System.out.println("百度搜索加载失败");
            }
        });

        //方法二:不开启线程,指定超时时间5秒,如果超时,则抛出异常
        //缺点:会阻塞进程
        try {
    
    
            ElementTimeOutUtils.waitElement(driver, By.id("content_bottoms"),5000);
            System.out.println("百度搜索加载成功");
        } catch (ElementTimeOutException e) {
    
    
            System.out.println("百度搜索加载失败");
            e.printStackTrace();
        }

    }

operation result:
Insert picture description here

  • The same can also be used to determine whether the account password is wrong when logging in, etc.
TimeOutUtil.waitElementText(driver, By.id("show_error"),"验证码错误", new TimeOutUtil.MyRunnable() {
    
    
            @Override
            public void run() {
    
    
                System.out.println("验证码错误");
            }
        });

Guess you like

Origin blog.csdn.net/qq_31254489/article/details/108267115