Selenium IE WebDriver仅在调试时有效

我使用的是Java Gradle,Selenium 3.8.0和IEWebDriver 3.8.0。

Chrome和Firefox工作正常,但IE抛出一​​个org.openqa.selenium.TimeoutException: Expected condition failedexception,虽然IE也可以正常工作,如果我一步一步调试我的源代码。

因此,我花了很长时间才发现这个问题,并且我注意到IE在WebDriver和源代码之间失去连接,每当webDriver.get(..) ,它看起来像这样:

 driver.get(url); waitForPageLoaded(driver); 

因此我假设有一些时间问题,但我已经尝试处理这个:

 public void waitForPageLoaded(WebDriver driver) { logger.debug("Wait until the page was loaded."); // IE seems to fail here. new WebDriverWait(driver, SeleniumConfigurator.TIME_OUT) .until(d -> ((JavascriptExecutor)d).executeScript("return document.readyState") .equals("complete")); } 

然后我注意到IE需要更多配置设置,但我不允许设置其中一些:IT限制 – >我无法更改regedit条目。

但是,为什么它在调试时工作正常?

这是我的IE设置:

 case IE: path = "../../../../../../resources/driver/win/IEDriverServer_32_v3-8-0.exe"; url = getClass().getResource(path); if (url == null) { logger.error("Could not find the Internet Explorer web driver binary at " + path + " ." + "All test for this browser will be ignored."); currentBrowserType = BrowserType.UNDEFINED; break; } try { System.setProperty("webdriver.ie.driver", Paths.get(url.toURI()).toFile().getAbsolutePath()); } catch (URISyntaxException e) { e.printStackTrace(); } // https://sqa.stackexchange.com/questions/13077/unable-to-run-selenium-webdriver-script-in-ie11 InternetExplorerOptions optionsIE = new InternetExplorerOptions(); optionsIE.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true); optionsIE.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); optionsIE.withAttachTimeout(SeleniumConfigurator.TIME_OUT, TimeUnit.SECONDS); //optionsIE.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true); webDriver = new InternetExplorerDriver(optionsIE); currentBrowserType = BrowserType.IE; break; 

我不知道这里出了什么问题..

第一个测试工作正常,之后出现超时exception(看一下注释):

 @Test public void test_Contact() { Init(); util.logTestStart("Test contact on index page.."); String xPath = "//*[@id='contact-link']/a"; WebElement element = webDriver.findElement(By.xpath(xPath)); Assert.assertEquals(element.getAttribute("href"), "mailto:what@ever.com"); } @Test public void test_LegalInformation() { Init(); util.logTestStart("Test legal information on index page.."); String xPath = "//*[@id='link-highlighted']/a"; util.aTagClickByXPath(webDriver, xPath); Assert.assertEquals(webDriver.getCurrentUrl(), "http://whatever.com/"); } private void Init() { if (configurator == null) { configurator = SeleniumConfigurator.getInstance(); } if (webDriver != configurator.getWebDriver()) { webDriver = configurator.getWebDriver(); } if (util == null) { util = new SeleniumTestUtil(); } // Open localhost as default util.goTo(webDriver, "http://localhost:8080/de/index"); } public void aTagClickByXPath(WebDriver driver, String xPath) { logger.debug("Performing a click on an a-Tag, xPath: " + xPath); WebElement element = driver.findElement(By.xpath(xPath)); element.click(); // First click works, second one fails, cause of Timeout Exception waitForPageLoaded(driver); } 

有人有提示吗?

编辑:

org.openqa.selenium.NoSuchWindowException: Unable to get browser被抛出。 超时exception不再出现了。 我一无所获。

EDIT2:

更多信息:

节点:

  

超时定义:

 public static final int TIME_OUT = 15; 

您可能需要考虑以下几个事实:

  • 首先将function()作为public void waitForPageLoaded(WebDriver driver)看作纯粹的开销。 基本上不需要在WebDriverWait上编写单独的包装函数
  • 根据Selenium v3.8.1WebDriverWait的当前实现, 构造函数如下:

     WebDriverWait(WebDriver driver, Clock clock, Sleeper sleeper, long timeOutInSeconds, long sleepTimeOut) WebDriverWait(WebDriver driver, long timeOutInSeconds) WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis) 

目前还不清楚你是如何实现WebDriverWait(driver, SeleniumConfigurator.TIME_OUT) 。 参数看起来容易出错。

同样,直到条件d -> ((JavascriptExecutor)d).executeScript("return document.readyState").equals("complete")是一个开销,因为Client (即Web Browser )永远不会将控制权返回给WebDriver实例,直到‘document.readyState’等于“完成” 。 一旦满足此条件, Selenium执行下一行代码。 因此, Boolean org.openqa.selenium.support.ui.FluentWait.until(Function arg0)这个函数没有任何影响。

值得一提的是,虽然Client (即Web Browser )可以在“document.readyState”等于“完成”时将控件返回给WebDriver实例,但它并不会占用新HTML DOM上的所有WebElements HTML DOMCLICKABLECLICKABLE和可CLICKABLE

  • 最后,为了解决您的主要问题,我需要澄清节点xPath = "//*[@id='link-highlighted']/a"以确保调用click()是否打开新选项卡或url被重定向。 我没有看到你处理任何一个案件。

  • 在处理InternetExplorer ,请记住InternetExplorerDriver在真实浏览器中运行并支持Javascript。
  • 设置浏览器焦点

     capabilities.setCapability("requireWindowFocus", true); 
  • 如果click()打开一个新窗口,请切换 window_handles