简体   繁体   English

Selenium IE WebDriver 仅在调试时有效

[英]Selenium IE WebDriver only works while debugging

I am using Java Gradle, Selenium 3.8.0 and IEWebDriver 3.8.0.我正在使用 Java Gradle、Selenium 3.8.0 和 IEWebDriver 3.8.0。

Chrome and Firefox are working fine, but IE throws a org.openqa.selenium.TimeoutException: Expected condition failed Exception, although IE also works fine, if I debug my source code step by step. Chrome 和 Firefox 工作正常,但 IE 抛出org.openqa.selenium.TimeoutException: Expected condition failed Exception,尽管 IE 也可以正常工作,如果我一步一步调试我的源代码。

Therefore I debuged a long time to find that problem and I noticed that IE looses the connection between WebDriver and Source Code, whenever a webDriver.get(..) is called, whichs looks like that:因此,我调试了很长时间才发现该问题,并且我注意到 IE 会在调用webDriver.get(..)时断开 WebDriver 和源代码之间的连接,如下所示:

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

Because of that I assume that there are some timing issues, but I already tried to handle this:因此,我认为存在一些时间问题,但我已经尝试处理了这个问题:

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"));
    }

Then I noticed that IE needs some more configuration settings, but I am not allowed to setup some of them: IT restrictions -> I cannot change regedit entries.然后我注意到 IE 需要一些更多的配置设置,但我不允许设置其中的一些:IT 限制 -> 我不能更改 regedit 条目。

BUT, why does it work fine, while debugging?但是,为什么在调试时它可以正常工作?

This is my IE setup:这是我的 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;

I have no idea what's going wrong here..我不知道这里出了什么问题..

The first test works fine, after that the timeout exception appears (take a look at the comment):第一个测试工作正常,之后出现超时异常(看看评论):

@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);
    }

Does anyone have a hint?有人有提示吗?

EDIT:编辑:

org.openqa.selenium.NoSuchWindowException: Unable to get browser get thrown for now. org.openqa.selenium.NoSuchWindowException: Unable to get browser Timeout Exception didnt appears anymore.超时异常不再出现。 I changed nothing.我什么都没变。

EDIT2:编辑2:

Further information:更多信息:

Node:节点:

<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>

Timeout definition:超时定义:

public static final int TIME_OUT = 15;

There are a couple of facts which you may have to consider as follows :您可能需要考虑以下几个事实:

  • First of all:首先:

     public void waitForPageLoaded(WebDriver driver)

    looks to me as a pure overhead.在我看来,纯粹是开销。 There is basically no need to write a separate wrapper function on top of WebDriverWait .基本上不需要在WebDriverWait之上编写单独的包装函数。

  • As per the current implementation of WebDriverWait in Selenium v3.8.1 the Constructors are as follows :根据Selenium v​​3.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)

    It is pretty much unclear how you have implemented:目前还不清楚您是如何实施的:

     WebDriverWait(driver, SeleniumConfigurator.TIME_OUT)

    The arguments looks error prone.参数看起来容易出错。

  • Again, the until condition再次,直到条件

    d -> ((JavascriptExecutor)d).executeScript("return document.readyState").equals("complete")

    is a overhead because the Client (ie the Web Browser ) will never return the control back to the WebDriver instance until and unless 'document.readyState' is equal to "complete" .是一种开销,因为客户端(即Web 浏览器)永远不会将控制权返回给WebDriver实例,除非'document.readyState'等于"complete" Once this condition is fulfilled Selenium performs the next line of code.一旦满足这个条件, Selenium 就会执行下一行代码。 Hence the function因此函数

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

    will have no impact.不会有影响。

  • It's worth to mention that though the Client (ie the Web Browser ) can return back the control to the WebDriver instance once 'document.readyState' equal to "complete" is achieved, it doesn't guarantees that all the WebElements on the new HTML DOM are VISIBLE , INTERACTABLE and CLICKABLE .值得一提的是,虽然客户端(即Web 浏览器)可以在实现'document.readyState' equal to "complete"后将控制权返回给WebDriver实例,但它并不能保证新HTML上的所有WebElements DOM可见的可交互点击

  • Finally, to address your main issue, I needed a clarification about the node xPath = "//*[@id='link-highlighted']/a" to ensure whether invoking click() opens a new tab or url gets redirected.最后,为了解决您的主要问题,我需要对节点xPath = "//*[@id='link-highlighted']/a"进行澄清,以确保调用click()打开一个新选项卡还是重定向 url。 I don't see you handling either of the cases.我没有看到你处理任何一种情况。


Solution解决方案

  • While dealing with InternetExplorer , keep in mind InternetExplorerDriver runs in a real browser and supports Javascript.在处理InternetExplorer 时,请记住InternetExplorerDriver在真实浏览器中运行并支持 Javascript。
  • Set the Browser Focus through :通过以下方式设置浏览器焦点

     capabilities.setCapability("requireWindowFocus", true);
  • If click() opens a new window, switch() through the window_handles如果click()打开一个新窗口, switch()通过window_handles


References参考

You can find a couple of relevant detailed discussions in:您可以在以下位置找到一些相关的详细讨论:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM