无法使用最新的Selenium驱动程序+ Chrome驱动程序启动带有扩展名的URL

时间:2019-05-03 07:52:10

标签: java selenium

Chrome驱动程序在添加扩展程序时被卡住/无法启动网址

ChromeBrowser 74.0.3729.131(正式版)(64位)
ChromeDriver 74.0.3729.6
SeleniumDriver selenium-server-standalone-3.141.59

public class ChromeBrowser {

    public static void main(String args[]){

        try{
            //Code working with out extensions
            System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            driver.get("http://www.google.com");

            //Code failed with extensions
            ChromeOptions options = new ChromeOptions();
            options.addExtensions(new File("./drivers/modheader_2_1_2.crx"));
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability(ChromeOptions.CAPABILITY, options);
            ChromeDriver drivers = new ChromeDriver(capabilities);
            drivers.get("http://www.google.com");
        }
        catch(Exception e){
            System.out.println("Exception caught :: "+e.getMessage());
        }

    }
}

2 个答案:

答案 0 :(得分:0)

添加扩展是chrome功能,因此您可以对这两行进行打折

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);

并通过

ChromeDriver drivers = new ChromeDriver(options);

到ChromeDriver

答案 1 :(得分:0)

如果您是Maven项目,则添加chromedriver依赖项可能会解决您的问题

如果您的硒版本为3.14.0,请尝试添加chromedriver依赖项3.14.0版本

https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver/3.14.0

这对我有用,因为以前版本的chromedriver不具有适用于最新chrome驱动程序的功能或已更改。如果您的测试失败并显示类似NoSuchMethodError的信息,请尝试此解决方案