使用 Internet Explorer 驱动程序在 Microsoft Edge 中自动执行 IE 模式 - Microsoft Edge Developer documentation
如果你有业务关键型旧版网站或应用,则可能需要在 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 。
必需配置
若要正确配置 IEDriver、Windows 和 Microsoft Edge,请完成 Selenium 所需的配置要求。
将驱动程序可执行文件放在 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”。
C#
Python
Java
JavaScript
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
namespace IEDriverSample
{
class Program
{
static void Main(string[] args)
{
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();
}
})();
以下部分更详细地介绍了此示例中的步骤。
使用 Microsoft Edge 的其他属性定义 InternetExplorerOptions
使用指向 Microsoft Edge 浏览器的其他属性进行定义 InternetExplorerOptions 。
C#
Python
Java
JavaScript
通过调用 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。
使用 值true和 ieOptions.setEdgePath() Microsoft Edge 可执行文件的路径调用 ieOptions.setEdgeChromium() 。
let ieOptions = new Options();
ieOptions.setEdgeChromium(true);
ieOptions.setEdgePath('C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe');
启动 IEDriver
启动 IEDriver。 IEDriver 启动 Microsoft Edge,然后在 IE 模式下加载 Web 内容。
C#
Python
Java
JavaScript
启动 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