【问题标题】:Selenium chromedriver opens a blank page instead of url on linuxSelenium chromedriver 在 linux 上打开一个空白页面而不是 url
【发布时间】:2018-06-17 08:06:51
【问题描述】:

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

我在调用 driver.get(url) 后截取了屏幕截图。它显示一个空白页。

east-northamptonshire_screenshot.jpg

我在本地机器 (Windows) 上尝试了这个(使用 ChromeDriver 打开一个 url)。它工作正常。

这是我要打开的网址。 "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

【问题讨论】:

    标签: java linux selenium selenium-chromedriver


    【解决方案1】:

    我认为是因为您的 Linux 机器上的 Chrome 版本不受支持。

    【讨论】:

    • 我可以打开其他网址。只有少数 url 导致了这个问题。这是其中一个网址的
    【解决方案2】:

    对于使用新版本 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 机器上却不行)

    【讨论】:

      【解决方案3】:

      在研究了证书错误之后,我在 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-10
        • 1970-01-01
        • 2021-08-15
        • 1970-01-01
        • 1970-01-01
        • 2018-02-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多