IE 特定功能

这些是 Microsoft Internet Explorer 浏览器特有的功能和特性.

自2022年6月起, Selenium 正式不再支持独立的 Internet Explorer. Internet Explorer 驱动程序仍支持在 “IE 兼容模式” 下运行 Microsoft Edge.

特别注意事项

IE 驱动程序是 Selenium 项目直接维护的唯一驱动程序. 虽然 32 位和 64 位版本的版本的二进制文件, 但有一些64位驱动程序的 已知限制. 因此, 建议使用 32 位驱动程序.

有关使用 Internet Explorer 的其他信息, 请参见 IE 驱动程序服务器页面

选项

在 Internet Explorer 兼容模式下启动 Microsoft Edge 浏览器, 并使用基本定义的选项, 看起来就像这样:

37
42
Show full example
package dev.selenium.browsers;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerDriverLogLevel;
import org.openqa.selenium.ie.InternetExplorerDriverService;
import org.openqa.selenium.ie.InternetExplorerOptions;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;

@EnabledOnOs(OS.WINDOWS)
public class InternetExplorerTest {
    public InternetExplorerDriver driver;
    private File logLocation;
    private File tempDirectory;

    @AfterEach
    public void quit() {
        if (logLocation != null && logLocation.exists()) {
            logLocation.delete();
        }
        if (tempDirectory  != null && tempDirectory.exists()) {
            tempDirectory.delete();
        }

        driver.quit();
    }

    @Test
    public void basicOptionsWin10() {
        InternetExplorerOptions options = new InternetExplorerOptions();
        options.attachToEdgeChrome();
        options.withEdgeExecutablePath(getEdgeLocation());
        driver = new InternetExplorerDriver(options);
    }

    @Test
    public void basicOptionsWin11() {
        InternetExplorerOptions options = new InternetExplorerOptions();
        driver = new InternetExplorerDriver(options);
    }

    @Test
    public void logsToFile() throws IOException {
        InternetExplorerDriverService service = new InternetExplorerDriverService.Builder()
                .withLogFile(getLogLocation())
                .build();

        driver = new InternetExplorerDriver(service);

        String fileContent = new String(Files.readAllBytes(getLogLocation().toPath()));
        Assertions.assertTrue(fileContent.contains("Started InternetExplorerDriver server"));
    }

    @Test
    public void logsToConsole() throws IOException {
        System.setOut(new PrintStream(getLogLocation()));

        InternetExplorerDriverService service = new InternetExplorerDriverService.Builder()
                .withLogOutput(System.out)
                .build();

        driver = new InternetExplorerDriver(service);

        String fileContent = new String(Files.readAllBytes(getLogLocation().toPath()));
        Assertions.assertTrue(fileContent.contains("Started InternetExplorerDriver server"));
    }

    @Test
    public void logsWithLevel() throws IOException {
        System.setProperty(InternetExplorerDriverService.IE_DRIVER_LOGFILE_PROPERTY,
                getLogLocation().getAbsolutePath());

        InternetExplorerDriverService service = new InternetExplorerDriverService.Builder()
                .withLogLevel(InternetExplorerDriverLogLevel.WARN)
                .build();

        driver = new InternetExplorerDriver(service);

        String fileContent = new String(Files.readAllBytes(getLogLocation().toPath()));
        Assertions.assertTrue(fileContent.contains("Invalid capability setting: timeouts is type null"));
    }

    @Test
    public void supportingFilesLocation() throws IOException {
        InternetExplorerDriverService service = new InternetExplorerDriverService.Builder()
                .withExtractPath(getTempDirectory())
                .build();

        driver = new InternetExplorerDriver(service);
        Assertions.assertTrue(new File(getTempDirectory() + "/IEDriver.tmp").exists());
    }

    private File getLogLocation() throws IOException {
        if (logLocation == null || !logLocation.exists()) {
            logLocation = File.createTempFile("iedriver-", ".log");
        }

        return logLocation;
    }

    private File getTempDirectory() throws IOException {
        if (tempDirectory == null || !tempDirectory.exists()) {
            tempDirectory = Files.createTempDirectory("supporting-").toFile();
        }

        return tempDirectory;
    }

    private String getEdgeLocation() {
        return System.getenv("EDGE_BIN");
    }
}
10
15
<details class="mt-3">
  <summary>Show full example</summary>
  <div class="pt-2">
    <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-py" data-lang="py"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">os</span>

import subprocess import sys import pytest from selenium import webdriver @pytest.mark.skipif(sys.platform != "win32", reason="requires Windows") def test_basic_options_win10(edge_bin): options = webdriver.IeOptions() options.attach_to_edge_chrome = True options.edge_executable_path = edge_bin driver = webdriver.Ie(options=options) driver.quit() @pytest.mark.skipif(sys.platform != "win32", reason="requires Windows") def test_basic_options_win11(): options = webdriver.IeOptions() driver = webdriver.Ie(options=options) driver.quit() @pytest.mark.skipif(sys.platform != "win32", reason="requires Windows") def test_file_upload_timeout(): options = webdriver.IeOptions() options.file_upload_timeout = 2000 driver = webdriver.Ie(options=options) driver.quit() @pytest.mark.skipif(sys.platform != "win32", reason="requires Windows") def test_ensure_clean_session(): options = webdriver.IeOptions() options.ensure_clean_session = True driver = webdriver.Ie(options=options) driver.quit() @pytest.mark.skipif(sys.platform != "win32", reason="requires Windows") def test_ignore_zoom_level(): options = webdriver.IeOptions() options.ignore_zoom_level = True driver = webdriver.Ie(options=options) driver.quit() @pytest.mark.skipif(sys.platform != "win32", reason="requires Windows") def test_ignore_protected_mode_settings(): options = webdriver.IeOptions() options.ignore_protected_mode_settings = True driver = webdriver.Ie(options=options) driver.quit() @pytest.mark.skipif(sys.platform != "win32", reason="requires Windows") def test_silent(): service = webdriver.IeService(service_args=["–silent"]) driver = webdriver.Ie(service=service) driver.quit() @pytest.mark.skipif(sys.platform != "win32", reason="requires Windows") def test_cmd_options(): options = webdriver.IeOptions() options.add_argument("-private") driver = webdriver.Ie(options=options) driver.quit() # Skipping this as it fails on Windows because the value of registry setting in # HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\TabProcGrowth must be '0' @pytest.mark.skip def test_force_create_process_api(): options = webdriver.IeOptions() options.force_create_process_api = True driver = webdriver.Ie(options=options) driver.quit() @pytest.mark.skipif(sys.platform != "win32", reason="requires Windows") def test_log_to_file(log_path): service = webdriver.IeService(log_output=log_path, log_level="INFO") driver = webdriver.Ie(service=service) with open(log_path, "r") as fp: assert "Starting WebDriver server" in fp.readline() driver.quit() @pytest.mark.skipif(sys.platform != "win32", reason="requires Windows") def test_log_to_stdout(capfd): service = webdriver.IeService(log_output=subprocess.STDOUT) driver = webdriver.Ie(service=service) out, err = capfd.readouterr() assert "Started InternetExplorerDriver server" in out driver.quit() @pytest.mark.skipif(sys.platform != "win32", reason="requires Windows") def test_log_level(log_path): service = webdriver.IeService(log_output=log_path, log_level="WARN") driver = webdriver.Ie(service=service) with open(log_path, "r") as fp: assert "Started InternetExplorerDriver server (32-bit)" in fp.readline() driver.quit() @pytest.mark.skipif(sys.platform != "win32", reason="requires Windows") def test_supporting_files(temp_dir): service = webdriver.IeService(service_args=["–extract-path=" + temp_dir]) driver = webdriver.Ie(service=service) driver.quit()

<div class="text-end pb-2 mt-2">
  <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/python/tests/browsers/test_internet_explorer.py#L11-L14" target="_blank">
    <i class="fas fa-external-link-alt pl-2"></i>
    <strong>View full example on GitHub</strong>
  </a>
</div>
34
39
<details class="mt-3">
  <summary>Show full example</summary>
  <div class="pt-2">
    <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cs" data-lang="cs"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">using</span> <span style="color:#000">System</span><span style="color:#000;font-weight:bold">;</span>

using System.IO; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium.IE; using SeleniumDocs.TestSupport; namespace SeleniumDocs.Browsers { [TestClassCustom] [EnabledOnOs("WINDOWS")] public class InternetExplorerTest { private InternetExplorerDriver _driver; private string _logLocation; private string _tempPath; [TestCleanup] public void Cleanup() { if (_logLocation != null && File.Exists(_logLocation)) { File.Delete(_logLocation); } if (_tempPath != null && File.Exists(_tempPath)) { File.Delete(_tempPath); } _driver.Quit(); } [TestMethod] public void BasicOptionsWin10() { var options = new InternetExplorerOptions(); options.AttachToEdgeChrome = true; options.EdgeExecutablePath = GetEdgeLocation(); _driver = new InternetExplorerDriver(options); } [TestMethod] public void BasicOptionsWin11() { var options = new InternetExplorerOptions(); _driver = new InternetExplorerDriver(options); } [TestMethod] [Ignore("Not implemented")] public void LogsToFile() { var service = InternetExplorerDriverService.CreateDefaultService(); service.LogFile = GetLogLocation(); _driver = new InternetExplorerDriver(service); _driver.Quit(); // Close the Service log file before reading var lines = File.ReadLines(GetLogLocation()); Console.WriteLine("Lines: {0}", lines); Assert.IsTrue(lines.Contains("Started InternetExplorerDriver server")); } [TestMethod] [Ignore("Not implemented")] public void LogsToConsole() { var stringWriter = new StringWriter(); var originalOutput = Console.Out; Console.SetOut(stringWriter); var service = InternetExplorerDriverService.CreateDefaultService(); //service.LogToConsole = true; _driver = new InternetExplorerDriver(service); Assert.IsTrue(stringWriter.ToString().Contains("geckodriver INFO Listening on")); Console.SetOut(originalOutput); stringWriter.Dispose(); } [TestMethod] public void LogsLevel() { var service = InternetExplorerDriverService.CreateDefaultService(); service.LogFile = GetLogLocation(); service.LoggingLevel = InternetExplorerDriverLogLevel.Warn; _driver = new InternetExplorerDriver(service); _driver.Quit(); // Close the Service log file before reading var lines = File.ReadLines(GetLogLocation()); Assert.IsNotNull(lines.FirstOrDefault(line => line.Contains("Invalid capability setting: timeouts is type null"))); } [TestMethod] public void SupportingFilesLocation() { var service = InternetExplorerDriverService.CreateDefaultService(); service.LibraryExtractionPath = GetTempDirectory(); _driver = new InternetExplorerDriver(service); Assert.IsTrue(File.Exists(GetTempDirectory() + "/IEDriver.tmp")); } private string GetLogLocation() { if (_logLocation == null || !File.Exists(_logLocation)) { _logLocation = Path.GetTempFileName(); } return _logLocation; } private string GetTempDirectory() { if (_tempPath == null || !File.Exists(_tempPath)) { _tempPath = Path.GetTempPath(); } return _tempPath; } private string GetEdgeLocation() { return Environment.GetEnvironmentVariable("EDGE_BIN"); } } }

<div class="text-end pb-2 mt-2">
  <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/dotnet/SeleniumDocs/Browsers/InternetExplorerTest.cs#L35-L38" target="_blank">
    <i class="fas fa-external-link-alt pl-2"></i>
    <strong>View full example on GitHub</strong>
  </a>
</div>