如果你有业务关键型旧版网站或应用,则可能需要在 Microsoft Edge 的 Internet Explorer (IE) 模式下测试内容。 本文介绍如何开始使用 Internet Explorer 驱动程序 (IEDriver) 在 Microsoft Edge 中自动执行 IE 模式。

Microsoft Edge 中的 IE 模式是仍需要 Internet Explorer 11 以向后兼容旧网站或应用的组织的一项功能。 若要了解有关 IE 模式的详细信息,请阅读 什么是 Internet Explorer (IE) 模式?

2022 年 6 月 15 日起,某些版本的 Windows 10 将不再支持 Internet Explorer 11。 有关详细信息,请阅读 Internet Explorer 11 桌面应用停用常见问题解答

下载 Internet Explorer 驱动程序 (IEDriver)

若要在 Microsoft Edge 中开始在 IE 模式下自动执行测试, 请下载 IEDriver 。 确保下载的 IEDriver 版本或更高版本 4.0.0.0

将驱动程序可执行文件放在 PATH 中

驱动程序可执行文件需要放在 PATH 中;请参阅 IE 驱动程序服务器 。 该页面的顶部显示:“独立服务器可执行文件必须从”下载“页下载并放置在 PATH 中。”

如果驱动程序位置未包含在 PATH 中,则必须使用 Java 系统属性 webdriver.ie.driver 或其他某种方式设置驱动程序位置。

在 Microsoft Edge 中自动执行 IE 模式

以下部分将引导你使用 Selenium 在 Microsoft Edge 中自动执行 IE 模式。

本文提供有关使用 Selenium 框架的说明,但你可以使用支持 WebDriver 的任何库、框架和编程语言。 若要使用其他框架完成相同的任务,请参阅所选框架的文档。

若要使用 IEDriver 在 IE 模式下启动 Microsoft Edge,请执行以下作:

  • 使用指向 Microsoft Edge 浏览器的其他属性进行定义 InternetExplorerOptions

  • 启动 的 InternetExplorerDriver 实例并传递它 InternetExplorerOptions 。 IEDriver 启动 Microsoft Edge,然后在 IE 模式下加载 Web 内容。

    下一部分演示完整示例,后续部分重点介绍上面列出的每个main步骤。

    以下示例在 IE 模式下启动 Microsoft Edge,导航到 bing.com ,然后搜索“WebDriver”。

    var ieOptions = new InternetExplorerOptions(); ieOptions.AttachToEdgeChrome = true; //change the path accordingly ieOptions.EdgeExecutablePath = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"; var driver = new InternetExplorerDriver(ieOptions); driver.Url = "https://bing.com"; driver.FindElement(By.Id("sb_form_q")).SendKeys("WebDriver"); driver.FindElement(By.Id("sb_form")).Submit(); driver.Quit();
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    ie_options = webdriver.IeOptions()
    ie_options.attach_to_edge_chrome = True
    ie_options.edge_executable_path = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"
    driver = webdriver.Ie(options=ie_options)
    driver.get("http://www.bing.com")
    elem = driver.find_element(By.ID, 'sb_form_q')
    elem.send_keys('WebDriver' + Keys.RETURN)
    driver.quit()
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.openqa.selenium.ie.InternetExplorerOptions;
    public class IEDriverSample {
        public static void main(String[] args) {        
            InternetExplorerOptions ieOptions = new InternetExplorerOptions();
            ieOptions.attachToEdgeChrome();
            ieOptions.withEdgeExecutablePath("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe");
            WebDriver driver = new InternetExplorerDriver(ieOptions);
            driver.get("http://www.bing.com");
            WebElement elem = driver.findElement(By.id("sb_form_q"));
            elem.sendKeys("WebDriver", Keys.RETURN);
            driver.close();
    
    const {Builder, By, Key, until} = require('selenium-webdriver');
    const {Options} = require('selenium-webdriver/ie');
    (async () => {
      let ieOptions = new Options();
      ieOptions.setEdgeChromium(true);
      ieOptions.setEdgePath('C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe');
      let driver = await new Builder().
                             forBrowser('ie').
                             setIeOptions(ieOptions).
                             build();
      try {
        await driver.get('http://www.bing.com');
        let elem = await driver.findElement(By.id('sb_form_q'));
        await elem.sendKeys('WebDriver', Key.RETURN);
        await driver.wait(until.titleIs('WebDriver - Bing'), 1000);
      } finally {
        await driver.quit();
    })();
    
  • 通过调用 InternetExplorerOptions()定义新变量 ieOptions

  • 将 属性设置为 true,并将 ieOptions.EdgeExecutablePath 设置为 ieOptions.AttachToEdgeChrome Microsoft Edge 可执行文件的路径。

    var ieOptions = new InternetExplorerOptions();
    ieOptions.AttachToEdgeChrome = true;
    //change the path accordingly
    ieOptions.EdgeExecutablePath = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe";
    
  • 通过调用 webdriver.IeOptions()定义新变量 ie_options

  • ie_options.attach_to_edge_chrome 属性设置为 True,并将 ie_options.edge_executable_path 设置为 Microsoft Edge 可执行文件的路径。

    ie_options = webdriver.IeOptions()
    ie_options.attach_to_edge_chrome = True
    ie_options.edge_executable_path = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"
    
  • 通过调用 new InternetExplorerOptions()定义 类型的InternetExplorerOptions新变量ieOptions

  • 使用 Microsoft Edge 可执行文件的路径调用 ieOptions.attachToEdgeChrome()ieOptions.withEdgeExecutablePath()

    InternetExplorerOptions ieOptions = new InternetExplorerOptions();
    ieOptions.attachToEdgeChrome();
    ieOptions.withEdgeExecutablePath("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe");
    
  • 通过调用 Options()定义新变量 ieOptions

  • 使用 值trueieOptions.setEdgePath() Microsoft Edge 可执行文件的路径调用 ieOptions.setEdgeChromium()

    let ieOptions = new Options();
    ieOptions.setEdgeChromium(true);
    ieOptions.setEdgePath('C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe');
    

    启动 InternetExplorerDriver 并传递之前定义的 ieOptions。 IEDriver 在 IE 模式下启动 Microsoft Edge。 所有页面导航和后续交互都在 IE 模式下进行。

    var driver = new InternetExplorerDriver(ieOptions);
    

    通过调用 webdriver.Ie 并传递之前定义的 ie_options来启动 IEDriver。 IEDriver 在 IE 模式下启动 Microsoft Edge。 所有页面导航和后续交互都在 IE 模式下进行。

    driver = webdriver.Ie(options=ie_options)
    

    通过调用 new InternetExplorerDriver() 并传递之前定义的 ieOptions来启动 IEDriver。 IEDriver 在 IE 模式下启动 Microsoft Edge。 所有页面导航和后续交互都在 IE 模式下进行。

    WebDriver driver = new InternetExplorerDriver(ieOptions);
    

    通过调用 Builder.forBrowser('ie')setIeoptions(ieOptions)启动 IEDriver。 IEDriver 在 IE 模式下启动 Microsoft Edge。 所有页面导航和后续交互都在 IE 模式下进行。

    let driver = await new Builder().
                           forBrowser('ie').
                           setIeOptions(ieOptions).
                           build();
    

    本部分介绍以前使用 IEDriver 和 IE11 桌面应用程序,但在 IE 模式下将 IEDriver 与 Microsoft Edge 配合使用时需要解决方法的已知方案。

    打开新窗口

    如果测试代码使用以下方法之一创建新的浏览器窗口,则之后可能需要添加一个短暂的等待作,以确保 IEDriver 检测到新窗口:

  • 通过在页面中调用 window.open from <script> 来打开新窗口。
  • 使用 WebDriver “新建 窗口”命令打开一个新窗口。
  • 若要确保新窗口已成功创建且 IEDriver 检测到它,必须持续检查“获取窗口句柄”命令的结果,直到它包含新窗口的句柄。

    以下示例演示了一种在打开新窗口时等待检测到新窗口句柄的可能方法。

    Click在打开新窗口的按钮上调用 方法后,测试代码必须等待,直到driver.WindowHandles包含新的窗口句柄。

    var initialHandleCount = driver.WindowHandles.Count;
    driver.FindElement(By.Id("<Id of the button that will open a new window>")).Click();
    var newHandles = driver.WindowHandles;
    while (newHandles.Count == initialHandleCount)
        newHandles = driver.WindowHandles;
                  click在打开新窗口的按钮上调用 方法后,测试代码必须等待,直到driver.window_handles包含新的窗口句柄。

    initial_handle_count = len(driver.window_handles)
    driver.find_element(By.ID, "<Id of the button that will open a new window>").click()
    new_handles = driver.window_handles
    while len(new_handles) == initial_handle_count:
        new_handles = driver.window_handles
                  click在打开新窗口的按钮上调用 方法后,测试代码必须等待,直到driver.getWindowHandles()包含新的窗口句柄。

    int initialHandleCount = driver.getWindowHandles().size();
    driver.findElement(By.id("<Id of the button that will open a new window>")).click();        
    Set<String> newHandles = driver.getWindowHandles();
    while (newHandles.size() == initialHandleCount) {
        newHandles = driver.getWindowHandles();
    

    click 打开新窗口的按钮上调用 方法后,IEDriver 必须使用 等待 await driver.getAllWindowHandles()

    const initialHandleCount = (await driver.getAllWindowHandles()).length;
    const elem = await driver.findElement(By.id("<Id of the button that will open a new window>"));
    await elem.click();
    let newHandles = await driver.getAllWindowHandles();
    while (newHandles.length == initialHandleCount) {
        newHandles = await driver.getAllWindowHandles();
    

    创建选项卡并在选项卡之间切换

    如果测试代码在同一Microsoft Edge 窗口中的多个选项卡之间切换,则变为非活动的选项卡可能不会包含在 “获取窗口句柄”返回的句柄列表中。 在 Internet Explorer 11 桌面应用程序中,无论激活状态如何,IEDriver 都将返回 IE 中所有选项卡的句柄。

    在 IE 模式下使用 Microsoft Edge 时,如果测试将焦点从某个选项卡移开,并且你希望以后能够切换回该选项卡,则必须存储选项卡窗口句柄的副本。

    使用 WebDriver 自动执行 Microsoft Edge - 概述如何使用 WebDriver 协议自动执行 Microsoft Edge。
  • Selenium 文档 - 有关 Selenium 上下文中的 WebDriver 的信息,以及如何使用 Selenium 编写自动化 WebDriver 测试。
  • 请联系 Microsoft Edge DevTools 团队 ,发送有关使用 WebDriver、WebDriver 测试框架 ((如 Selenium) 和 Microsoft Edge)的反馈。
  •