Selenium chromedriver在linux上打开一个空白页而不是url

时间:2018-06-17 08:06:52

标签: java linux selenium selenium-chromedriver

我正在尝试在linux上使用chromedriver(使用RemoteWebDriver)打开一个url。

我在调用driver.get(url)后拍了一张截图。它显示一个空白页面。

east-northamptonshire_screenshot.jpg

我在本地计算机(Windows)上尝试过此操作(使用ChromeDriver打开网址)。它工作正常。

这是我想要打开的网址。 “https://publicaccess.east-northamptonshire.gov.uk/online-applications/search.do?action=weeklyList

主要方法:

 public static void main(String[] args) throws Exception {

    String OS = System.getProperty("os.name").toLowerCase();

    WebDriver driver = null;
    ChromeDriverService service = null;

    boolean isWindows = OS.indexOf("win") >= 0;
    logger.info("operating System : " + OS);
    if (!isWindows) {
        service = new ServerChromeDriver().loadService();
    }
    driver = new ServerChromeDriver().getPIDriver(service, isWindows);

    String url = "https://publicaccess.east-northamptonshire.gov.uk/online-applications/search.do?action=weeklyList";
    driver.get(url);

    Thread.sleep(3000);
    ScreenShot.takeScreenShot(driver);

    driver.close();
    driver.quit();
    service.stop();
}

ServerChromeDriver类:

  public ChromeDriverService loadService() throws Exception {

    Configuration configuration = new Configuration();
    configuration.loadProperties();

    Properties props = new Properties();
    try {
        props.load(new FileInputStream("config//log4j.properties"));
    } catch (IOException e) {
        logger.error(e);
    }
    PropertyConfigurator.configure(props);

    service = new ChromeDriverService.Builder().usingDriverExecutable(new File(configuration.getChromeDriverPath()))
            .usingAnyFreePort().withEnvironment(ImmutableMap.of("DISPLAY", configuration.getDisplay())).build();
    service.start();

    return service;
}

  public WebDriver getPIDriver(ChromeDriverService service, boolean isWindows) {

    WebDriver driver;

    if (isWindows) {
        driver = new LocalChromeDriver().getDriver();
    } else {
        driver = new ServerChromeDriver().getDriver(service.getUrl());
    }

    String hostName = new ServerChromeDriver().getHostName(driver);
    logger.info("Running the application on host: " + hostName);

    return driver;
}

public WebDriver getDriver(URL serviceUrl) {

    Configuration configuration = new Configuration();
    configuration.loadProperties();
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--headless");
    chromeOptions.addArguments("--disable-gpu");
    // chromeOptions.addArguments("--start-maximized");
    chromeOptions.addArguments("--window-size=1800,1800");
    // chromeOptions.addExtensions(new File(configuration.getAdBlockPath()));


    System.setProperty("webdriver.chrome.driver", configuration.getChromeDriverPath());
    System.setProperty("webdriver.chrome.logfile", configuration.getChromeDriverLogFilePath());
    System.setProperty("webdriver.chrome.verboseLogging", configuration.getChromeVerboseLogging());

    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
    capabilities.setJavascriptEnabled(true);

    try {
        driver = new RemoteWebDriver(serviceUrl, capabilities);
    } catch (Exception e) {
        logger.error("Error creating a new chrome instance");
        throw new RuntimeException(e.getMessage());
    }
    driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS);

    return driver;
}

应用程序正在为此网址付费:https://eplanning.birmingham.gov.uk/Northgate/PlanningExplorer/GeneralSearch.aspx

birmingham_screenshot.jpg

我正在使用

Headless Chrome : 67.0.3396.62
chromedriver    : 2.40.565383 

这是我从chromedriver.log文件中找到的

[0617/144457.403693:ERROR:nss_ocsp.cc(601)] No URLRequestContext for NSS HTTP handler. host: crt.comodoca.com
[0617/144457.403801:ERROR:cert_verify_proc_nss.cc(980)] CERT_PKIXVerifyCert for publicaccess.east-northamptonshire.gov.uk failed err=-8179

3 个答案:

答案 0 :(得分:1)

我认为因为不支持Linux机器中的Chrome版本。

答案 1 :(得分:1)

对于使用较新版本的Selenium来解决此问题的人们,我能够执行以下操作(类似于Abhilash的回答,即使用较旧版本的Selenium)来解决未认证域在Chrome中的空白页问题:

ChromeOptions options = new ChromeOptions();    
...
options.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
...
driver = new ChromeDriver(options);

(在我的情况下,webdriver在我的本地计算机上运行良好,但在托管CI工具的Linux机器上运行不正常)

答案 2 :(得分:0)

在研究证书错误后,我为chrome驱动程序添加了其他功能,以忽略与证书相关的错误。以下是我的解决方案。

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();

        capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
        capabilities.setJavascriptEnabled(true);
        capabilities.setCapability(CapabilityType.PROXY, proxy);
        capabilities.setCapability("acceptSslCerts", true); // Added this additionally
        capabilities.setCapability("acceptInsecureCerts", true); // Added this additionally
        capabilities.setCapability("ignore-certificate-errors", true); // Added this additionally