zoukankan      html  css  js  c++  java
  • selenium_java

    等待

    页面全加载

    ChromeOptions options=new ChromeOptions();
    /**
    *(1) NONE: 当html下载完成之后,不等待解析完成,selenium会直接返回
    *(2) EAGER: 要等待整个dom树加载完成,即DOMContentLoaded这个事件完成,仅对html的内容进行下载解析
    *(3) NORMAL: 即正常情况下,selenium会等待整个界面加载完成(指对html和子资源的下载与解析,如JS文件,图片等,不包括ajax)
    */
    options.setPageLoadStrategy(PageLoadStrategy.NONE);
    //添加参数,关闭拦截弹窗
    options.addArguments("disable-popup-blocking");
    options.addArguments("disable-notifications");
    //关闭被自动化控制的提示
    options.addArguments("disable-infobars");
    options.addArguments("disable-extensions");
    options.addArguments("start-maximized");
    
    driver = new ChromeDriver(options);
    
    private void Form1_Load(object sender, EventArgs e)
    {
        ChromeOptions options = new ChromeOptions();
        options.AddArguments("--disable-extensions"); // to disable extension
        options.AddArguments("--disable-notifications"); // to disable notification
        options.AddArguments("--disable-application-cache"); // to disable cache
        options.AddArgument("--safebrowsing-disable-download-protection");
        options.AddArgument("ignore-certificate-errors");
        options.AddArgument("--disable-popup-blocking");
        options.AddArgument("--disable-gpu");
        options.AddArgument("--incognito");
        options.AddUserProfilePreference("disable-popup-blocking", "true");
        driveri = new ChromeDriver(options); 
        driveri.Navigate().GoToUrl("http://web.com");
    
        IJavaScriptExecutor js = (IJavaScriptExecutor)driveri; 
        js.ExecuteScript("window.alert = function() {};");
    }
    

    显示等待50秒

    WebElement ele_14 = (new WebDriverWait(webDriver, 50)).until(ExpectedConditions.elementToBeClickable(By.xpath(SIT_Objects.tab_MPF)));
    //visibilityOfElementLocated
    
        public boolean isPresent(String xpath, int waitingTimeInSec) {
            try {
                WebDriverWait wait = new WebDriverWait(webDriver, waitingTimeInSec);
                wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));
            } catch (TimeoutException e) {
                return false;
            }
            return true;
        }
    
    

    隐式等待

    driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS); //与显式等待同时存在时,以其中最大值为准。
    

    js

    driver = new ChromeDriver(options);
    Javascript = ((JavascriptExecutor) driver);
    boolean jsReady=(Boolean)Javascript.executeScript("return document.readyState").toString().equals("complete");
    while(!jsReady){
        Thread.sleep(100);
    }
    
    //Wait for jQuery to load
            {
                ExpectedCondition<Boolean> jQueryLoad = driver -> ((Long) ((JavascriptExecutor) driver).executeScript("return jQuery.active") == 0);
                boolean jqueryReady = (Boolean) js.executeScript("return jQuery.active==0");
                if (!jqueryReady) {
    //System.out.println("JQuery is NOT Ready!");
                    wait.until(jQueryLoad);
                }
                wait.until(jQueryLoad);
            }
    //Wait for ANGULAR to load
            {
                String angularReadyScript = "return angular.element(document).injector().get('$http').pendingRequests.length === 0";
                ExpectedCondition<Boolean> angularLoad = driver -> Boolean.valueOf(((JavascriptExecutor) driver).executeScript(angularReadyScript).toString());
                boolean angularReady = Boolean.valueOf(js.executeScript(angularReadyScript).toString());
                if (!angularReady) {
    //System.out.println("ANGULAR is NOT Ready!");
                    wait.until(angularLoad);
                }
            }
    //Wait for Javascript to load
            {
                ExpectedCondition<Boolean> jsLoad = driver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").toString()
                        .equals("complete");
                boolean jsReady = (Boolean) js.executeScript("return document.readyState").toString().equals("complete");
    //Wait Javascript until it is Ready!
                if (!jsReady) {
    //System.out.println("JS in NOT Ready!");
                    wait.until(jsLoad);
                }
            }
    

    刷新

    driver.navigate().refresh();
    

    代理

    Proxy proxy = new Proxy();
    proxy.setNoProxy("connections.manulife.com;intranet01.manulife.com;ifshome.manulife.com;adfs.man;ulife.com;sts.manulife.com;ctx.manulife.com;mlilapp00.manulife.com;*.manulife.;net;centralsharepoint;205.*.*.*;209.*.*.*;192.168.*.*;10.*.*.*;*.phbdomain.loc;al;*.manulifeusa.com;ato.manulife.com;hkitsp.ap.manulife.com;connections.manul;ife.com;ars7.manulife.com;*.ap.manulife.com;globalgrc.manulife.com;*.mfcgd.com;*.pcf.manulife.com;*.gsdcf.manulife.com;giam.manulife.com;*.mbpscd.com;*.plat;form.manulife.io;*.manulife.io;Ifsgl.manulife.com;*.mbpscd.com;join.poolweb.ma;nulife.com;20.139.52.44;*.japan.corp.manulife.com;*.mliph.manulife.com;*.mliph.manulife.com.ph;set4success.manulife.com;digipasshk.manulife.com;digipasskl.manulife.com;citrixstore.jhancock.com;mywcasd01.dc1.manulife.net.my");
    options.setProxy(proxy);
    

    Refer

    https://cloud.tencent.com/developer/article/1497428

  • 相关阅读:
    2019年春阅读笔记13——分布式系统的两种方式
    2019年春阅读笔记12——索引数据结构设相关的计算机原理
    2019年春阅读笔记11——常见的查询算法及数据结构
    2019年春阅读笔记10——数据库优化
    2019年春阅读笔记9——一些基础优化
    2019年春阅读笔记8——从不同层面进行SQL优化
    2019年春阅读笔记7——关于SQL优化
    2019年春阅读笔记6——继续说开源
    jQuery使用小技巧
    marquee 实现首尾相连循环滚动效果
  • 原文地址:https://www.cnblogs.com/liehen2046/p/11346551.html
Copyright © 2011-2022 走看看