簡體   English   中英

Selenium IE WebDriver 僅在調試時有效

[英]Selenium IE WebDriver only works while debugging

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

Chrome 和 Firefox 工作正常,但 IE 拋出org.openqa.selenium.TimeoutException: Expected condition failed Exception,盡管 IE 也可以正常工作,如果我一步一步調試我的源代碼。

因此,我調試了很長時間才發現該問題,並且我注意到 IE 會在調用webDriver.get(..)時斷開 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 超時異常不再出現。 我什么都沒變。

編輯2:

更多信息:

節點:

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

您可能需要考慮以下幾個事實:

  • 首先:

     public void waitForPageLoaded(WebDriver driver)

    在我看來,純粹是開銷。 基本上不需要在WebDriverWait之上編寫單獨的包裝函數。

  • 根據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)

    目前還不清楚您是如何實施的:

     WebDriverWait(driver, SeleniumConfigurator.TIME_OUT)

    參數看起來容易出錯。

  • 再次,直到條件

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

    是一種開銷,因為客戶端(即Web 瀏覽器)永遠不會將控制權返回給WebDriver實例,除非'document.readyState'等於"complete" 一旦滿足這個條件, Selenium 就會執行下一行代碼。 因此函數

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

    不會有影響。

  • 值得一提的是,雖然客戶端(即Web 瀏覽器)可以在實現'document.readyState' equal to "complete"后將控制權返回給WebDriver實例,但它並不能保證新HTML上的所有WebElements DOM可見的可交互點擊

  • 最后,為了解決您的主要問題,我需要對節點xPath = "//*[@id='link-highlighted']/a"進行澄清,以確保調用click()打開一個新選項卡還是重定向 url。 我沒有看到你處理任何一種情況。


解決方案

  • 在處理InternetExplorer 時,請記住InternetExplorerDriver在真實瀏覽器中運行並支持 Javascript。
  • 通過以下方式設置瀏覽器焦點

     capabilities.setCapability("requireWindowFocus", true);
  • 如果click()打開一個新窗口, switch()通過window_handles


參考

您可以在以下位置找到一些相關的詳細討論:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM