java - 在 Windows10 环境中使用 RemoteWebDriver 和 Internet Explorer 11 时 HTML 无效 (Selenium/Java)

标签 java selenium internet-explorer remotewebdriver

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

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

测试源代码片段

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。

最佳答案

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

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

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

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

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

enter image description here

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

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

您可以尝试在您这边检查一下,并让我们知道它是否适合您。

关于java - 在 Windows10 环境中使用 RemoteWebDriver 和 Internet Explorer 11 时 HTML 无效 (Selenium/Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62298045/

相关文章:

java - 如何确定文件是否从 svn 补丁文件中添加/删除/修改

java - jdbc 更新查询无法使用 derby 数据库和 SpringJUnit4ClassRunner 进行单元测试

java - JSF 2 使用 @ManagedProperty 注入(inject) Spring bean/service 而没有 xml

javascript - 带有 Xvfb 的 Headless Selenium - JavaScript 不工作,但没有 <noscript>

internet-explorer - 如何从 Excel VBA 保存网页中的文件?

java - 如何通过其 int 值迭代枚举?

JavaScript 执行器在 Google Chrome 中工作,但在 Firefox 中不起作用

ruby - 使用 Capybara 和 Selenium 如何执行 SHIFT+ 单击

javascript - 在 Internet Explorer 的新窗口中运行 javascript 代码

css - 表格边框颜色在 IE 中不起作用