【问题标题】:Selenium IE WebDriver only works while debuggingSelenium IE WebDriver 仅在调试时工作
【发布时间】:2018-05-22 09:31:41
【问题描述】:

我正在使用 Java Gradle、Selenium 3.8.0 和 IEWebDriver 3.8.0。

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

因此我调试了很长时间才发现这个问题,我注意到每当调用webDriver.get(..) 时,IE 都会失去 WebDriver 和源代码之间的连接,看起来像这样:

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;

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

第一次测试正常,之后出现超时异常(看评论):

@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 暂时被抛出。超时异常不再出现。我什么都没改变。

EDIT2:

更多信息:

节点:

<div class="col-xs-12" id="link-container">
                <div id="bike-link" class="pull-right">
                    <a href="http://whatever.com/?lang=D">
                        whatever
                        <i class="fa fa-chevron-right" aria-hidden="true"></i>
                    </a>
                </div>
                <div id="link-highlighted" class="pull-right">
                    <a href="http://whatever2.com/"> <!-- this one -->
                         Rechtliche Hinweise
                        <i class="fa fa-chevron-right" aria-hidden="true"></i>
                    </a>
                </div>
                <div id="contact-link" class="pull-right">
                    <a href="mailto:what@ever.com">
                        Kontakt
                        <i class="fa fa-chevron-right" aria-hidden="true"></i>
                    </a>
                </div>
            </div>

超时定义:

public static final int TIME_OUT = 15;

【问题讨论】:

  • 你能告诉我们节点String xPath = "//*[@id='link-highlighted']/a";吗?
  • 当然,我更新了问题;请参阅:编辑 2

标签: java selenium selenium-webdriver internet-explorer-11


【解决方案1】:

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

  • 首先:

    public void waitForPageLoaded(WebDriver driver)
    

    在我看来是纯粹的开销。基本上不需要在WebDriverWait之上写一个单独的封装函数。

  • 根据Selenium v​​3.8.1中WebDriverWait的当前实现,构造函数如下:

    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 浏览器)永远不会将控制权返回给 WebDriver 实例,除非 'document.readyState' 等于"complete"。一旦满足此条件Selenium 执行下一行代码。因此函数

    Boolean org.openqa.selenium.support.ui.FluentWait.until(Function<? super WebDriver, Boolean> arg0)
    

    不会有影响。

  • 值得一提的是,虽然 Client(即 Web 浏览器)可以将控制权返回给 WebDriver 实例,一旦 'document.readyState' equal to "complete"已实现,但不能保证新 HTML DOM 上的所有 WebElements 都是可见、可交互和可点击。

  • 最后,为了解决您的主要问题,我需要对节点 xPath = "//*[@id='link-highlighted']/a" 进行说明,以确保调用 click() 是否会打开新选项卡或 url 被重定向。我没有看到你处理这两种情况。


解决方案

  • 在处理 InternetExplorer 时,请记住 InternetExplorerDriver 在真正的浏览器中运行并支持 Javascript。
  • 通过以下方式设置浏览器焦点:

    capabilities.setCapability("requireWindowFocus", true);
    
  • 如果click()打开一个新窗口,switch()通过window_handles


参考文献

您可以在以下位置找到一些相关的详细讨论:

【讨论】:

  • 感谢您提供非常有用的信息。我会考虑的。
  • 我知道,但 IE 仍然不想工作,虽然我考虑过你的帮助。
猜你喜欢
  • 2016-02-16
  • 2015-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-27
  • 1970-01-01
  • 2019-07-10
相关资源
最近更新 更多