首页 > 解决方案 > Selenium WebDriver,在加载 HTTPS 网站时,会关闭并重新打开浏览器,甚至不会尝试与 DOM 交互

问题描述

Selenium WebDriver(从 Eclipse Oxygen.3a Release (4.7.3a) 运行)似乎无法处理任何 HTTPS 网站。

我的意思是:加载页面时(无论是否重定向,都尝试使用 gitlab 和 AWS 登录站点)

最令人沮丧的是,控制台显示没有错误。这里:铬:

Jul 03, 2019 8:40:44 AM com.fincad.vcr.qa.support.WebDriverFactory createWebDriver

INFO: Web driver is created successfully

Jul 03, 2019 8:40:55 AM com.fincad.vcr.qa.support.WebDriverFactory quitWebDriver

INFO: Web driver quits successfully

就是这样,就像字面意思一样(注意 15 秒的间隔?那是 browserWaitTimeout)。

a)我尝试了 FF,在那里我看到了 Marionette 的错误:

1562172157538   Marionette  INFO    Listening on port 56792

1562172157572   addons.xpi-utils    DEBUG   Successfully read XPI database

1562172157603   addons.manager  DEBUG   Registering upgrade listener for 
formautofill@mozilla.org

Jul 03, 2019 9:42:37 AM org.openqa.selenium.remote.ProtocolHandshake createSession

INFO: Detected dialect: W3C

Jul 03, 2019 9:42:38 AM com.fincad.vcr.qa.support.WebDriverFactory createWebDriver

INFO: Web driver is created successfully

1562172170219   Marionette  INFO    Stopped listening on port 56792

1562172170243   addons.xpi  DEBUG   Calling bootstrap method shutdown on webcompat@mozilla.org version 4.0.0

1562172170249   addons.xpi  DEBUG   Calling bootstrap method shutdown on screenshots@mozilla.org version 37.1.0

1562172170253   addons.xpi  DEBUG   Calling bootstrap method shutdown on fxmonitor@mozilla.org version 3.0

1562172170254   addons.xpi  DEBUG   Calling bootstrap method shutdown on formautofill@mozilla.org version 1.0

[Parent 12824, Gecko_IOThread] WARNING: pipe error: 109: file z:/task_1560988628/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 341

[Child 21788, Chrome_ChildThread] WARNING: pipe error: 109: file z:/task_1560988628/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 341

[Child 21788, Chrome_Chi[Parent 12824, Gecko_IOThread] WARNING: pipe error: 109: file z:/task_1560988628/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 341

[Child 6644, Chrome_ChildThread] WARNING: pipe error: 109: file z:/task_1560988628/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 341

[Child 6644, Chrome_ChildThread] WAR[Parent 12824, Gecko_IOThread] WARNING: pipe error: 109: file z:/task_1560988628/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 341

[Child 17844, Chrome_ChildThread] WARNING: pipe error: 109: file z:/task_1560988628/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 341

[Child 17844, Chrome_ChildThread] WARNING: pipe er[Parent 12824, Gecko_IOThread] WARNING: pipe error: 109: file z:/task_1560988628/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 341

[Chi###!!! [Parent][MessageChannel] Error: (msgtype=0x1F0099,name=PBrowser::Msg_UpdateNativeWindowHandle) Closed channel: cannot send/recv

我用谷歌搜索,人们提到这是由于 FF 驱动程序过时,所以我升级了,但它没有任何好处(出现同样的错误)。在 IE 中,它就像 Chrome:根本没有错误或警告消息。

我的驱动程序是: Selenium WebDriver: 3.14.0 (32-bit) geckodriver 0.24.0 (32-bit) chromedriver 74.0.3729.6 (32-bit) IEDriverServer 3.8.0 (32-bit)

我的浏览器: Chrome 浏览器:75.0.3770.100 FF 浏览器:67.0.4 IE 浏览器:11.557.17763

这就是我创建驱动程序的方式:

private static void createFirefoxDriver() {
    setCapability("firefox");
    GeckoDriverService.Builder builder = new GeckoDriverService.Builder();
    GeckoDriverService service = builder.build();
    FirefoxOptions options = new FirefoxOptions(capabilities);
    // Read the default firefox profile
    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile myProfile = profile.getProfile("default");
    // Disable the web page from asking if really want to leave
    myProfile.setPreference("dom.disable_beforeunload", true);
    options.setProfile(myProfile);
    driver = new FirefoxDriver(service, options);
}

private static void createChromeDriver() {
    setCapability("chrome");
    ChromeDriverService.Builder builder = new ChromeDriverService.Builder();
    ChromeDriverService service = builder.build();
    ChromeOptions options = new ChromeOptions();
    options.merge(capabilities);
    driver = new ChromeDriver(service, options);
}

这就是我加载 URL(本地网络驱动程序)的方式:

private static void createLocalWebDriver() {
    String browser = ConfigParser.getBrowser();
    LOGGER.info("Target Browser: " + browser);

    switch (browser) {
        case "firefox":
            System.setProperty("webdriver.gecko.driver", ConfigParser.getGeckoDriver());
            System.out.println("GeckoDriver on: " + ConfigParser.getGeckoDriver());
            createFirefoxDriver();
            break;
        case "chrome":
            System.setProperty("webdriver.chrome.driver", ConfigParser.getChromeDriver());
            System.out.println("ChromeDriver on: " + ConfigParser.getChromeDriver());
            createChromeDriver();
            break;
        case "ie":
            System.setProperty("webdriver.ie.driver", ConfigParser.getIEDriverServer());
            System.out.println("IEDriver on: " + ConfigParser.getIEDriverServer());
            createIEDriver();
            break;
        default:
            LOGGER.warning("Unsupported Browser: " + browser);
            break;
    }
}
private static void createFirefoxDriver() {`enter code here`
    setCapability("firefox");
    GeckoDriverService.Builder builder = new GeckoDriverService.Builder();
    GeckoDriverService service = builder.build();
    FirefoxOptions options = new FirefoxOptions(capabilities);
    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile myProfile = profile.getProfile("default");
    myProfile.setPreference("dom.disable_beforeunload", true);
    options.setProfile(myProfile);
    driver = new FirefoxDriver(service, options);
}

private static void createChromeDriver() {
    setCapability("chrome");
    ChromeDriverService.Builder builder = new ChromeDriverService.Builder();
    ChromeDriverService service = builder.build();
    ChromeOptions options = new ChromeOptions();
    options.merge(capabilities);
    driver = new ChromeDriver(service, options);
}

private static void createIEDriver() {
    setCapability("ie");
    InternetExplorerDriverService.Builder builder = new InternetExplorerDriverService.Builder();
    InternetExplorerDriverService service = builder.build();
    InternetExplorerOptions options = new InternetExplorerOptions(capabilities);
    driver = new InternetExplorerDriver(service, options);
    CommonJS.executeScript(driver, "window.localStorage.clear();");
    CommonJS.executeScript(driver, "window.sessionStorage.clear();");
}

private static void setCapability(String browser) {
    capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
    capabilities.setCapability(CapabilityType.ELEMENT_SCROLL_BEHAVIOR, true);
    if (browser.equalsIgnoreCase("ie")) {
        capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
        capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
        capabilities.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true);
        capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, ConfigParser.getAppUrl());
    } else if (browser.equalsIgnoreCase("chrome")) {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("chrome.switches","--disable-extensions");
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    }
}

对不起所有的代码,但我相信我会被问到这些细节,因此我放在那里。长话短说:

标签: eclipseseleniumselenium-webdriverhttpswebdriver

解决方案


这是页面加载代码的问题。找到一个明确要求仅 HTTP 的部分,因此拒绝加载任何 HTTPS URL。


推荐阅读