【问题标题】:Invalid HTML when using RemoteWebDriver and Internet Explorer 11 in a Windows10 environment (Selenium/Java)在Windows10环境中使用RemoteWeBdriver和Internet Explorer 11时无效的HTML(Selenium / Java)
【发布时间】:2020-09-29 13:00:37
【问题描述】:

我开发了一些自动测试,它们在本地运行良好,但使用 RemoteWebDriver、IE11 失败。 该页面由 IFrame 组成,它只是我找不到元素的框架之一。其他框架工作正常。 我检查了该特定 IFrame 的 HTML,发现远程运行时 HTML 无效(driver.getPageSource())。它错过了几个关闭标签。 但是在本地运行时,HTML 是正确的。因此,无法到达元素。在使用 RemoteWebDriver 时,是否有人有可能使 HTML 有效的解决方案?

它与 chrome 完美配合(HTML 使用 RemoteWebDriver 有效)但不幸的是我被 Internet Explorer 卡住了。 :(

先决条件

  1. Windows 10
  2. Selenium-java 版本:3.141.59
  3. 驱动版本:IEDriverServer_Win32_3.141.5

示例:WebDriver(本地)

<li id="foldheader">Export Collection</li>
<ul id="foldinglist" style="display: none;">
    <li id="foldheader">EXCO Registration</li>
    <ul id="foldinglist" style="display: none;">
        <span>
            <li id="list">
                <a id="listdel" onmouseover="mOVER(this)" onmouseout="mOUT(this)" href="javascript:mCLICK(str);" substr="ID=D1901&amp;Module=Export Collection&amp;Group=EXCORegistration&amp;Function=Create Collection&amp;GroupId=X4820">Create Collection</a>
            </li>
        </span>
        <span>
            <li id="list">
                <a id="listdel" onmouseover="mOVER(this)" onmouseout="mOUT(this)" href="javascript:mCLICK(str);" substr="ID=D2336&amp;Module=Export Collection&amp;Group=EXCORegistration&amp;Function=Create Collection(Copy Existing)&amp;GroupId=X4820">Create Collection(Copy Existing)</a>
            </li>
        </span>
        <span>
            <li id="list">
                <a id="listdel" onmouseover="mOVER(this)" onmouseout="mOUT(this)" href="javascript:mCLICK(str);" substr="ID=D1929&amp;Module=Export Collection&amp;Group=EXCORegistration&amp;Function=Acknowledgement&amp;GroupId=X4820">Acknowledgement</a>
            </li>
        </span>
        <span>
            <li id="list">
                <a id="listdel" onmouseover="mOVER(this)" onmouseout="mOUT(this)" href="javascript:mCLICK(str);" substr="ID=D1886&amp;Module=Export Collection&amp;Group=EXCORegistration&amp;Function=Process MT410&amp;GroupId=X4820">Process T410</a>
            </li>
        </span>
        <span>
            <li id="list">
                <a id="listdel" onmouseover="mOVER(this)" onmouseout="mOUT(this)" href="javascript:mCLICK(str);" substr="ID=D3285&amp;Module=Export Collection&amp;Group=EXCORegistration&amp;Function=Process Collection from CE&amp;GroupId=X4820">Process Collection from CE</a>
            </li>
        </span>
    </ul>
</ul>

示例:RemoteWebDriver(缺少关闭标签)

<LI id=foldheader>Export Collection
    <UL id=foldinglist style="DISPLAY: none">
        <LI id=foldheader>EXCO Registration
            <UL id=foldinglist style="DISPLAY: none">
                <SPAN>
                    <LI id=list>
                        <A onmouseover=mOVER(this) onmouseout=mOUT(this) id=listdel href="javascript:mCLICK(str);" substr="ID=F05030701901&amp;Module=Export Collection&amp;Group=EXCORegistration&amp;Function=Create Collection&amp;GroupId=G49082300552">Create Collection</A>
                    </SPAN>
                    <SPAN>
                        <LI id=list>
                            <A onmouseover=mOVER(this) onmouseout=mOUT(this) id=listdel href="javascript:mCLICK(str);" substr="ID=F05030702336&amp;Module=Export Collection&amp;Group=EXCORegistration&amp;Function=Create Collection(Copy Existing)&amp;GroupId=G49082300552">Create Collection(Copy Existing)</A>
                        </SPAN>
                        <SPAN>
                            <LI id=list>
                                <A onmouseover=mOVER(this) onmouseout=mOUT(this) id=listdel href="javascript:mCLICK(str);" substr="ID=F05030701929&amp;Module=Export Collection&amp;Group=EXCORegistration&amp;Function=Acknowledgement&amp;GroupId=G49082300552">Acknowledgement</A>
                            </SPAN>
                            <SPAN>
                                <LI id=list>
                                    <A onmouseover=mOVER(this) onmouseout=mOUT(this) id=listdel href="javascript:mCLICK(str);" substr="ID=F05030701886&amp;Module=Export Collection&amp;Group=EXCORegistration&amp;Function=Process MT410&amp;GroupId=G49082300552">Process MT410</A>
                                </SPAN>
                                <SPAN>
                                    <LI id=list>
                                        <A onmouseover=mOVER(this) onmouseout=mOUT(this) id=listdel href="javascript:mCLICK(str);" substr="ID=F05030703285&amp;Module=Export Collection&amp;Group=EXCORegistration&amp;Function=Process Collection from CE&amp;GroupId=G49082300552">Process Collection from CE</A>
                                    </SPAN>
                                </LI>
                            </UL>
                        </UL>

测试源码sn-p

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.net.URL;
import java.sql.Driver;

public class IETest {
    private static void internetExplorerTest() {
        //WebDriver driver;
        RemoteWebDriver driver;
        URL url =null;
        try{
            url = new URL("<remote url>");
        } catch(Exception e) {

        }

        //Setting the webdriver.chrome.driver property to its executable's location
        System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer_Win32_3.141.5\\IEDriverServer.exe");

        InternetExplorerOptions ieOptions = new InternetExplorerOptions();
        ieOptions.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
        ieOptions.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true);

        //driver = new InternetExplorerDriver(ieOptions);
        driver = new RemoteWebDriver(url, ieOptions);

        driver.get("<url>");

        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("FunctionList")));
        driver.switchTo().frame("FunctionList");

        // Check HTML in frame (Returns invalid HTML when using RemoteWebDriver. Missing close-tags)
        System.out.println(driver.getPageSource());

        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[text()='Export Collection']"))).click();

        driver.quit();
    }

    public static void main (String[] args) {
        internetExplorerTest();
    }
}

很遗憾,由于安全原因,我无法为您提供 IFrame 中的整个 HTML。但是模式就像上面的 RemoteWebDriver HTML。缺少 li-close-tags。

【问题讨论】:

  • 看起来您已经发布了在本地 IE 中获得的输出和在远程 Web 驱动程序中获得的输出。我建议您发布您的硒代码和该 iframe 中的 HTML 代码。它可以帮助我们对其进行测试并检查问题。
  • @Deepak-MSFT 谢谢你的回答。我添加了一些测试代码,但不幸的是,由于安全原因,我无法上传整个 HTML。

标签: java selenium internet-explorer remotewebdriver


【解决方案1】:

我们可以看到您正在控制台中打印输出。

您正在获得输出,但它不完整。

源可能很大,但控制台缓冲区大小较小。由于这个原因,可能会出现此问题。

如果您使用的是 Eclipse,那么您可以参考以下步骤来增加 控制台缓冲区大小

  1. 转到窗口 > 首选项 > 运行/调试 > 控制台
  2. 增加控制台缓冲区大小
  3. 或者直接取消选中“限制控制台输出”

另一种解决方法是将数据写入文件。

File file = new File("path/filename.txt");
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();

参考:

Java selenium getPageSource not working

您可以尝试自己检查一下,让我们知道它是否适合您。

【讨论】:

  • 我之前确实这样做过。不幸的是,结果相同。
  • 您是否尝试将源代码保存在文件中?那次测试你得到了什么结果?
  • 我得到了同样的结果。执行 RemoteWebdriver 时源缺少结束标记。但是如果在本地执行(WebDriver),HTML 是有效的,没有丢失结束标签。
  • 您是否尝试在本地访问同一个网页并使用远程 Web 驱动程序?如果它是公共页面,那么您可以尝试在此处共享链接。我们可以尝试用 Remote web driver 测试一下,看看结果。
猜你喜欢
  • 2015-12-22
  • 2018-07-25
  • 2014-03-03
  • 2018-08-16
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2017-07-19
  • 2015-01-27
相关资源
最近更新 更多