【问题标题】:Waiting for IE's "This page can't be displayed" Page等待IE的“此页面无法显示”页面
【发布时间】:2015-06-22 20:52:40
【问题描述】:

我正在尝试使用 Selenium 创建一个显式等待,当无法访问某个页面时,它会等待 Internet Explorer 中“无法显示此页面”消息的可见性。当然,Selenium 在 xpath 中的撇号存在问题。我正在使用的代码如下...

String expectedText = "This page can't be displayed";
String waitforXpath = "//div[contains(.,'"+expectedText+"')]";
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(waitforXpath)));

当然,Selenium 会抛出 TimeoutException 等待“无法显示此页面”元素的可见性。有关如何最好地解决此问题的任何建议?

编辑:这是该页面的来源...

<!DOCTYPE HTML>
<html>

<head>
    <link rel="stylesheet" type="text/css" href="NewErrorPageTemplate.css" >

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>This page can't be displayed</title>

    <script src="errorPageStrings.js" language="javascript" type="text/javascript">
    </script>
    <script src="httpErrorPagesScripts.js" language="javascript" type="text/javascript">
    </script>
</head>

<body onLoad="javascript:getInfo();">
    <div id="contentContainer" class ="mainContent">
        <div id="mainTitle" class="title">This page can't be displayed</div>
        <div class="taskSection" id="taskSection">
            <ul id="cantDisplayTasks" class="tasks">
                <li id="task1-1">Make sure the web address <span id="webpage" class="webpageURL"></span>is correct.</li>
                <li id="task1-2">Look for the page with your search engine.</li>
                <li id="task1-3">Refresh the page in a few minutes.</li>
            </ul>
            <ul id="notConnectedTasks" class="tasks" style="display:none">
                <li id="task2-1">Check that all network cables are plugged in.</li>
                <li id="task2-2">Verify that airplane mode is turned off.</li>
                <li id="task2-3">Make sure your wireless switch is turned on.</li>
                <li id="task2-4">See if you can connect to mobile broadband.</li>
                <li id="task2-5">Restart your router.</li>
            </ul>
        </div>
        <div><button id="diagnose" class="diagnoseButton" onclick="javascript:diagnoseConnectionAndRefresh(); return false;">Fix connection problems</button></div>
    </div>
</body>

</html>

【问题讨论】:

  • 使用不同的 XPath、CSS 或其他任何东西......如果没有看到 HTML 的相关部分,我们无法帮助您。
  • 您尝试使用 ID By.Id("mainTitle") 吗?
  • @Sham 理想情况下,我希望指定“无法显示此页面”消息,因为任何页面都可以有一个 ID 为 mainTitle 的元素,不一定说“此页面无法显示”
  • 没错,你为什么不获取元素并检查它的文本!
  • @Sham 你如何将它合并到 WebDriver 等待中,就像我正在尝试做的那样?

标签: java internet-explorer selenium selenium-webdriver


【解决方案1】:

您可以通过像这样检查文本来大大简化您的问题:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("mainTitle"), "This page can't be displayed"));

【讨论】:

  • 似乎不起作用。仍然找不到元素。
  • “好像没用。”不是很明确。阅读stackoverflow.com/help/mcve
  • 它仍然会抛出 TimeoutException ... org.openqa.selenium.TimeoutException: Timed out after 10 seconds waiting for text ('This page can't be displayed') to be present in element found by By.id: mainTitle
  • 是的,我尝试过最长 60 秒的超时,但仍然抛出相同的异常。
  • 只是为了好玩和咯咯笑,尝试像10000这样的超时,看看它是否曾经出现。
【解决方案2】:

试试这个 for 循环逻辑。该代码会在等待 2 秒后获取页面标题,然后重试 5 次。

boolean isTitleFound= false;
int tryCount = 0;
while (!isTitleFound || (tryCount++)<5)

    try{
        Thread.sleep(2000)
    } catch (Exception e) {
    }

    if (driver.getTitle().equalsIgnoreCase("This page can't be displayed") {
        isTitleFound = true;
    }
}

【讨论】:

  • 是的,我考虑过这种方式,但如果可能的话,我真的很想尝试让它与 WebDriverWait 和 ExpectedConditions 类一起工作,因为毕竟这就是它们的用途。不过,这可能是破解它的唯一方法。
【解决方案3】:

任何时候你将一个值传递给一个 xpath 查询,你都必须处理那个值中的任何撇号。您尝试运行的实际 xpath 查询是;

//div[contains(.,'This page can't be displayed')]

你认为你可以逃脱它,但其他人和我过去在这样做时遇到了问题。

http://www.seleniumtests.com/2010/08/xpath-and-single-quotes.html

使用 concat 对我和上面链接中的人有效。我希望以下 xpath 可以工作。

String expectedText = "concat('This page can','\"'\"','t be displayed')";
String waitforXpath = "//div[contains(.,'"+expectedText+"')]";

一般来说,我倾向于通过一种方法将我插入到 xpath 中的任何值传递给将其转换为 xpath 就绪的 concat 字符串的方法。这是 c#。

private string prepareForXpath(string text)
{
    if (!text.Contains("'"))
    {
        return "'" + text + "'";
    }
    StringBuilder finalString = new StringBuilder();
    finalString.Append("concat('");
    finalString.Append(text.Replace("'", "',\"'\",'"));
    finalString.Append("')");
    return finalString.ToString();        
}

这一切都非常混乱,这就是为什么 xpath 定位器会很痛苦。我更喜欢SiKing建议的方法。这种方法行不通的事实可能值得更多调查。

【讨论】:

    【解决方案4】:

    原来 Selenium 在 IE 的“此页面无法显示”页面上实际上找不到任何元素,但在任何其他页面上都可以。它还能够在 Firefox 和 Chrome 各自的“问题加载页面”页面上找到元素。但是,它可以在 IE 中获取页面的标题,该标题等于我最初正在寻找的 mainTitle WebElement 的文本。所以我采取了以下解决方案......

    expectedText = "This page can't be displayed";
    WebDriverWait wait = new WebDriverWait(driverRp,10);
    wait.until(ExpectedConditions.titleContains(expectedText));
    

    我更改了问题的标题,以反映我遇到的问题的实际根源,因为根本问题与我的预期和最初询问的不同。在这个 IE/Selenium 交互中非常奇怪的行为。不知道为什么在“无法显示此页面”页面上找不到WebElements。有知道的请留言!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-13
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多