使用 selenium 时碰到的问题,麻烦知情人告知一下解决方案

系统:MAC OS

版本:10.13.6

当前用 selenium 模拟手机操作,UA 为华为安卓。程序在正常情况下表现良好,但是当切换到其它全屏窗口时,浏览器的执行将会出现等待,直到我切回浏览器所在的窗口,操作才会继续进行。
不明所已,特来提问

初始化代码如下,业务代码就不贴了,一个弄着玩的小东西

        System.setProperty("webdriver.chrome.driver", "/software/driver/chromedriver");
        final ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--user-agent=Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; BLA-AL00 Build/HUAWEIBLA-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.9 Mobile Safari/537.36");
        chromeOptions.addArguments("--no-sandbox");
        driver = new ChromeDriver(chromeOptions);
        driver.manage().window().setSize(new Dimension(W, H));

        wait = new WebDriverWait(driver, WAIT_INTEVAL);

自问自答

官方文档

        System.setProperty("webdriver.chrome.driver", ""youdir)
                // 声明ChromeOptions,主要是给Chrome设置参数.
        final ChromeOptions chromeOptions = new ChromeOptions();
        // 设置user agent的参数为iPhone 6
        chromeOptions.addArguments("--user-agent=iPhone 6");
        //使沙箱进程能够在不分配作业对象的情况下运行。需要此标志才能允许Chrome在RemoteApps或Citrix中运行。
        //此标志可以降低沙盒进程的安全性,并允许它们执行某些API调用,如关闭Windows或访问剪贴板。
        //此外,我们失去了杀死某些进程的机会,直到拥有它们的外部作业完成。
        chromeOptions.addArguments("--allow-no-sandbox-job");
        //浏览器以隐身模式直接启动
        chromeOptions.addArguments("--incognito");
        //去除【正在测试】的信息提示栏
        chromeOptions.addArguments("--disable-infobars");
        //表示浏览器处于“无需登录浏览”(访客会话)模式。应该完全禁用扩展,同步和书签。
        chromeOptions.addArguments("--bwsi");
        //禁用 GPU
        chromeOptions.addArguments("--disable-gpu");
        //禁用 gpu 早期的主动初始化
        chromeOptions.addArguments("--disable-gpu-early-init");
        //禁用堆栈跟踪
        chromeOptions.addArguments("--disable-in-process-stack-traces");
        driver = new ChromeDriver(chromeOptions);
        driver.manage().window().setSize(new Dimension(W, H));

        wait = new WebDriverWait(driver, WAIT_INTEVAL);


主要是这个配置起了作用

--allow-no-sandbox-job

如果那个网页有循环加载或者别的问题,就会一直等待,可以延迟+driver.close()关闭