Selenium WebDriver在加载HTTPS网站时会关闭并重新打开浏览器,甚至不会尝试与DOM进行交互

时间:2019-07-04 01:19:11

标签: eclipse selenium selenium-webdriver https webdriver

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

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

  • 登录页面正确加载(没有SSL证书问题,任何重定向都可以),但Selenium却不做任何事(调试模式表明它甚至没有尝试执行登录代码)
  • 超时(browserWaitTimeout = 15)并关闭/重新打开登录页面
  • 这种情况一直持续到第4次,Selenium最终到达我的登录代码(我设置的断点终于被击中),但是这种情况发生在浏览器页面关闭和重新打开之间,因此它从未检测到DOM并失败了。 li>

最令人沮丧的是,控制台未显示任何错误。 此处:Chrome:

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,从那里看到了木偶的错误:

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位) geckodriver 0.24.0(32位) chromedriver 74.0.3729.6(32位) IEDriverServer 3.8.0(32位)

和我的浏览器: 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);
    }
}

对不起,所有代码都可以,但是我敢肯定会询问这些细节,因此请放在那里。 长话短说:

  • 这仅在HTTPS URL的情况下发生

  • 非HTTPS URL的工作正常

  • 这不是SSL证书问题,否则我会在“此连接不受信任”屏幕上看到它。

1 个答案:

答案 0 :(得分:0)

页面加载代码时出现问题。找到了一个明确只希望使用HTTP的部分,因此拒绝加载任何HTTPS URL。