美文网首页我爱编程
【error】selenium启动chrome时,弹出设置页面:

【error】selenium启动chrome时,弹出设置页面:

作者: Joey_GZ | 来源:发表于2018-02-06 21:10 被阅读1051次

预期:用selenium启动chrome浏览器,自动打开百度页面
结果:启动chrome时同时打开两个标签页,且页面停留在chrome的设置页面
解决:通过配置参数禁止data;的出现,添加如下代码

// 通过配置参数禁止data;的出现
    options.addArguments("--user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data\\Default");
// 通过配置参数删除“您使用的是不受支持的命令行标记:--ignore-certificate-errors。稳定性和安全性会有所下降。”提示
    options.addArguments("--start-maximized", "allow-running-insecure-content", "--test-type");
chromeerror1.jpg chromeerror2.jpg

完整代码(参考网络资料整理)

package demo;


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class LaunchChrome {

    public static void main(String[] args) throws Exception {
        
        WebDriver driver = null;
        ChromeOptions options = new ChromeOptions();
        // 通过配置参数禁止data;的出现
        options.addArguments("--user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data\\Default");
        // 通过配置参数删除“您使用的是不受支持的命令行标记:--ignore-certificate-errors。稳定性和安全性会有所下降。”提示
        options.addArguments("--start-maximized", "allow-running-insecure-content", "--test-type");
        
        
        System.setProperty("webdriver.chrome.driver", "D:\\ChromeDownloads\\chromedriver.exe"); //设置启动谷歌驱动(如果版本对不上,会启动不成功)
        driver = new ChromeDriver(options);
        
        driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS); //设置规定时间内,等待网页完整加载完成  
        driver.get("https://www.baidu.com");
        
    
        System.out.println("当前打开页面的标题是: "+ driver.getTitle()); //getTitle()获取当前页面title的值
        Thread.sleep(1000);
        
        driver.quit(); //关闭并退出浏览器

        
    }

}

相关文章

网友评论

    本文标题:【error】selenium启动chrome时,弹出设置页面:

    本文链接:https://www.haomeiwen.com/subject/amrnzxtx.html