【问题标题】:How to launch chromebrowser using chrome options?如何使用 chrome 选项启动 chromebrowser?
【发布时间】:2018-11-12 06:42:41
【问题描述】:

不使用 System.setProperty

System.setProperty("webdriver.chrome.driver", "F:\\New folder\\chromedriver.exe");

我尝试使用带有以下代码的 chrome 选项启动 chrome 浏览器:

ChromeOptions options = new ChromeOptions();
        options.setAcceptInsecureCerts(true);
        options.addArguments("disable-infobars");

        driver = new ChromeDriver(options);

我已经在我的系统变量中设置了我的 chromedriver 路径。它不工作可以解决这个问题。

【问题讨论】:

    标签: java selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    您能否尝试将下面的代码设置为Binary 也放入Options 以启动chromeBrowser:

    ChromeOptions options = new ChromeOptions(); 
    options.setBinary("C:\\Users\\raheela.aslam\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");  //Path where the Chrome.exe exist in your machine.
    System.setProperty("webdriver.chrome.driver", "C:\\chrome_driver\\chromedriver.exe"); 
    driver = new ChromeDriver(options);
    

    【讨论】:

    • 不使用 system.setProperty 不能启动浏览器吗? @Raheela Aslam
    • 根据文档,我们需要提供 seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/… 其他选项是尝试使用最新的 Chrome 驱动程序。
    • 感谢您消除疑问,现在可以工作了,但是如何在我的电脑中找到 chrome 的二进制路径?我不是在询问 chrome 驱动程序的路径,而是在询问您包含的二进制路径。
    • 请像我在 Program Files "C:\Program Files (x86)\Google\Chrome\Application" 中一样找到 Chrome 的安装位置
    • 知道了,感谢清理
    【解决方案2】:

    还有另一种在不设置属性的情况下启动 chrome 的方法。这将下载最新的 chrome 驱动程序版本并启动它。您可以在使用 bonigarcia 依赖项中使用 WebDriverManager。

    bonigarcia dependency

    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    

    【讨论】:

    • 这是使用 Marven 对吗?在 pom.xml 中我们需要添加依赖项对吗?
    • 完全正确。将 bonigarcia 依赖项放入您的 pom.xml 并运行上面的简单代码将启动您的 chrome。你也可以为 Firefox 做。
    猜你喜欢
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    相关资源
    最近更新 更多