IE 特定功能

這些是 Microsoft Internet Explorer 瀏覽器特有的功能和特性。

截至 2022 年 6 月,Selenium 官方已不再支援獨立的 Internet Explorer。Internet Explorer 驅動程式仍然支援在「IE 相容模式」下執行 Microsoft Edge。

特殊考量

IE 驅動程式是唯一由 Selenium 專案直接維護的驅動程式。雖然 32 位元和 64 位元版本的 Internet Explorer 都有二進制檔案可用,但 64 位元驅動程式存在一些 已知限制。因此,建議使用 32 位元驅動程式。

關於使用 Internet Explorer 的更多資訊,請參閱 IE Driver Server 頁面

選項

在 Internet Explorer 相容模式下啟動 Microsoft Edge 瀏覽器,並使用基本定義的選項,看起來像這樣

        InternetExplorerOptions options = new InternetExplorerOptions();
        options.attachToEdgeChrome();
        options.withEdgeExecutablePath(getEdgeLocation());
        driver = new InternetExplorerDriver(options);
    options = webdriver.IeOptions()
    options.attach_to_edge_chrome = True
    options.edge_executable_path = edge_bin
    driver = webdriver.Ie(options=options)
            var options = new InternetExplorerOptions();
            options.AttachToEdgeChrome = true;
            options.EdgeExecutablePath = GetEdgeLocation();
            _driver = new InternetExplorerDriver(options);
      options = Selenium::WebDriver::IE::Options.new
      options.attach_to_edge_chrome = true
      options.edge_executable_path = edge_location
      @driver = Selenium::WebDriver.for :ie, options: options

從 Internet Explorer Driver v4.5.0 開始

  • 如果系統上沒有 IE(Windows 11 中的預設情況),則不需要使用上述兩個參數。IE Driver 將使用 Edge 並自動找到它。
  • 如果系統上同時存在 IE 和 Edge,您只需要設定附加到 Edge,IE Driver 將自動在您的系統上找到 Edge。

因此,如果系統上沒有 IE,您只需要

移動程式碼

        InternetExplorerOptions options = new InternetExplorerOptions();
        driver = new InternetExplorerDriver(options);
    options = webdriver.IeOptions()
    driver = webdriver.Ie(options=options)
            var options = new InternetExplorerOptions();
            _driver = new InternetExplorerDriver(options);
      options = Selenium::WebDriver::Options.ie
      @driver = Selenium::WebDriver.for :ie, options: options
let driver = await new Builder()
.forBrowser('internet explorer')
.setIEOptions(options)
.build();
val options = InternetExplorerOptions()
val driver = InternetExplorerDriver(options)

以下是一些具有不同功能的常見用例

fileUploadDialogTimeout

在某些環境中,Internet Explorer 在開啟「檔案上傳」對話方塊時可能會逾時。IEDriver 的預設逾時時間為 1000 毫秒,但您可以使用 fileUploadDialogTimeout 功能增加逾時時間。

移動程式碼

InternetExplorerOptions options = new InternetExplorerOptions();
options.waitForUploadDialogUpTo(Duration.ofSeconds(2));
WebDriver driver = new RemoteWebDriver(options);
  
    options = webdriver.IeOptions()
    options.file_upload_timeout = 2000
var options = new InternetExplorerOptions();
options.FileUploadDialogTimeout = TimeSpan.FromMilliseconds(2000);
var driver = new RemoteWebDriver(options);
  
      @options.file_upload_dialog_timeout = 2000
const ie = require('selenium-webdriver/ie');
let options = new ie.Options().fileUploadDialogTimeout(2000);
let driver = await Builder()
          .setIeOptions(options)
          .build(); 
  
val options = InternetExplorerOptions()
options.waitForUploadDialogUpTo(Duration.ofSeconds(2))
val driver = RemoteWebDriver(options)
  

ensureCleanSession

當設定為 true 時,此功能會清除所有正在執行的 InternetExplorer 實例(包括手動或由驅動程式啟動的實例)的快取、瀏覽器歷史記錄和 Cookie。預設情況下,它設定為 false

使用此功能會導致啟動瀏覽器時效能下降,因為驅動程式會在啟動 IE 瀏覽器之前等待快取清除完成。

此功能接受布林值作為參數。

移動程式碼

InternetExplorerOptions options = new InternetExplorerOptions();
options.destructivelyEnsureCleanSession();
WebDriver driver = new RemoteWebDriver(options);
  
    options = webdriver.IeOptions()
    options.ensure_clean_session = True
var options = new InternetExplorerOptions();
options.EnsureCleanSession = true;
var driver = new RemoteWebDriver(options);
  
      @options.ensure_clean_session = true
const ie = require('selenium-webdriver/ie');
let options = new ie.Options().ensureCleanSession(true);
let driver = await Builder()
          .setIeOptions(options)
          .build(); 
  
val options = InternetExplorerOptions()
options.destructivelyEnsureCleanSession()
val driver = RemoteWebDriver(options)
  

ignoreZoomSetting

InternetExplorer 驅動程式期望瀏覽器縮放層級為 100%,否則驅動程式將擲回例外。可以透過將 ignoreZoomSetting 設定為 true 來停用此預設行為。

此功能接受布林值作為參數。

移動程式碼

InternetExplorerOptions options = new InternetExplorerOptions();
options.ignoreZoomSettings();
WebDriver driver = new RemoteWebDriver(options);
  
    options = webdriver.IeOptions()
    options.ignore_zoom_level = True
var options = new InternetExplorerOptions();
options.IgnoreZoomLevel = true;
var driver = new RemoteWebDriver(options);
  
      @options.ignore_zoom_level = true
const ie = require('selenium-webdriver/ie');
let options = new ie.Options().ignoreZoomSetting(true);
let driver = await Builder()
          .setIeOptions(options)
          .build(); 
  
val options = InternetExplorerOptions()
options.ignoreZoomSettings()
val driver = RemoteWebDriver(options)
  

ignoreProtectedModeSettings

是否在啟動新的 IE 工作階段時跳過保護模式檢查。

如果未設定且所有區域的保護模式設定不相同,驅動程式將擲回例外。

如果功能設定為 true,則測試可能會變得不穩定、無回應,或瀏覽器可能會掛起。但是,這仍然是次佳選擇,而最佳選擇始終應該是實際手動設定每個區域的保護模式設定。如果使用者正在使用此屬性,則僅提供「盡力而為」的支援。

此功能接受布林值作為參數。

移動程式碼

InternetExplorerOptions options = new InternetExplorerOptions();
options.introduceFlakinessByIgnoringSecurityDomains();
WebDriver driver = new RemoteWebDriver(options);
  
    options = webdriver.IeOptions()
    options.ignore_protected_mode_settings = True
var options = new InternetExplorerOptions();
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
var driver = new RemoteWebDriver(options);
  
      @options.ignore_protected_mode_settings = true
const ie = require('selenium-webdriver/ie');
let options = new ie.Options().introduceFlakinessByIgnoringProtectedModeSettings(true);
let driver = await Builder()
          .setIeOptions(options)
          .build(); 
  
val options = InternetExplorerOptions()
options.introduceFlakinessByIgnoringSecurityDomains()
val driver = RemoteWebDriver(options)
  

silent

當設定為 true 時,此功能會抑制 IEDriverServer 的診斷輸出。

此功能接受布林值作為參數。

移動程式碼

InternetExplorerOptions options = new InternetExplorerOptions();
options.setCapability("silent", true);
WebDriver driver = new InternetExplorerDriver(options);
  
    service = webdriver.IeService(service_args=["--silent"])
    driver = webdriver.Ie(service=service)
InternetExplorerOptions options = new InternetExplorerOptions();
options.AddAdditionalInternetExplorerOption("silent", true);
IWebDriver driver = new InternetExplorerDriver(options);
  
      @options.silent = true
const {Builder,By, Capabilities} = require('selenium-webdriver');
let caps = Capabilities.ie();
caps.set('silent', true);

(async function example() {
    let driver = await new Builder()
        .forBrowser('internet explorer')
        .withCapabilities(caps)
        .build();
    try {
        await driver.get('http://www.google.com/ncr');
    }
    finally {
        await driver.quit();
    }
})();
  
import org.openqa.selenium.Capabilities
import org.openqa.selenium.ie.InternetExplorerDriver
import org.openqa.selenium.ie.InternetExplorerOptions

fun main() {
    val options = InternetExplorerOptions()
    options.setCapability("silent", true)
    val driver = InternetExplorerDriver(options)
    try {
        driver.get("https://google.com/ncr")
        val caps = driver.getCapabilities()
        println(caps)
    } finally {
        driver.quit()
    }
}
  

命令列選項

Internet Explorer 包含多個命令列選項,可讓您疑難排解和設定瀏覽器。

以下描述一些支援的命令列選項

  • -private:用於在私密瀏覽模式下啟動 IE。這適用於 IE 8 及更高版本。

  • -k:在 kiosk 模式下啟動 Internet Explorer。瀏覽器會在最大化的視窗中開啟,該視窗不顯示網址列、導航按鈕或狀態列。

  • -extoff:在無附加元件模式下啟動 IE。此選項專門用於疑難排解瀏覽器附加元件的問題。適用於 IE 7 及更高版本。

注意:forceCreateProcessApi 應啟用,命令列引數才能運作。

移動程式碼

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;

public class ieTest {
    public static void main(String[] args) {
        InternetExplorerOptions options = new InternetExplorerOptions();
        options.useCreateProcessApiToLaunchIe();
        options.addCommandSwitches("-k");
        InternetExplorerDriver driver = new InternetExplorerDriver(options);
        try {
            driver.get("https://google.com/ncr");
            Capabilities caps = driver.getCapabilities();
            System.out.println(caps);
        } finally {
            driver.quit();
        }
    }
}
  
    options = webdriver.IeOptions()
    options.add_argument("-private")

    driver = webdriver.Ie(options=options)
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;

namespace ieTest {
 class Program {
  static void Main(string[] args) {
   InternetExplorerOptions options = new InternetExplorerOptions();
   options.ForceCreateProcessApi = true;
   options.BrowserCommandLineArguments = "-k";
   IWebDriver driver = new InternetExplorerDriver(options);
   driver.Url = "https://google.com/ncr";
  }
 }
}
  
      @options.add_argument('-k')
const ie = require('selenium-webdriver/ie');
let options = new ie.Options();
options.addBrowserCommandSwitches('-k');
options.addBrowserCommandSwitches('-private');
options.forceCreateProcessApi(true);

driver = await env.builder()
          .setIeOptions(options)
          .build();
  
import org.openqa.selenium.Capabilities
import org.openqa.selenium.ie.InternetExplorerDriver
import org.openqa.selenium.ie.InternetExplorerOptions

fun main() {
    val options = InternetExplorerOptions()
    options.useCreateProcessApiToLaunchIe()
    options.addCommandSwitches("-k")
    val driver = InternetExplorerDriver(options)
    try {
        driver.get("https://google.com/ncr")
        val caps = driver.getCapabilities()
        println(caps)
    } finally {
        driver.quit()
    }
}
  

forceCreateProcessApi

強制使用 CreateProcess API 啟動 Internet Explorer。預設值為 false。

對於 IE 8 及更高版本,此選項需要將 “TabProcGrowth” 登錄值設定為 0。

移動程式碼

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;

public class ieTest {
    public static void main(String[] args) {
        InternetExplorerOptions options = new InternetExplorerOptions();
        options.useCreateProcessApiToLaunchIe();
        InternetExplorerDriver driver = new InternetExplorerDriver(options);
        try {
            driver.get("https://google.com/ncr");
            Capabilities caps = driver.getCapabilities();
            System.out.println(caps);
        } finally {
            driver.quit();
        }
    }
}
  
    options = webdriver.IeOptions()
    options.force_create_process_api = True

    driver = webdriver.Ie(options=options)
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;

namespace ieTest {
 class Program {
  static void Main(string[] args) {
   InternetExplorerOptions options = new InternetExplorerOptions();
   options.ForceCreateProcessApi = true;
   IWebDriver driver = new InternetExplorerDriver(options);
   driver.Url = "https://google.com/ncr";
  }
 }
}
  
      @options.force_create_process_api = true
const ie = require('selenium-webdriver/ie');
let options = new ie.Options();
options.forceCreateProcessApi(true);

driver = await env.builder()
          .setIeOptions(options)
          .build();
  
import org.openqa.selenium.Capabilities
import org.openqa.selenium.ie.InternetExplorerDriver
import org.openqa.selenium.ie.InternetExplorerOptions

fun main() {
    val options = InternetExplorerOptions()
    options.useCreateProcessApiToLaunchIe()
    val driver = InternetExplorerDriver(options)
    try {
        driver.get("https://google.com/ncr")
        val caps = driver.getCapabilities()
        println(caps)
    } finally {
        driver.quit()
    }
}
  

服務

所有瀏覽器通用的服務設定在 服務頁面 上描述。

記錄輸出

取得驅動程式日誌對於偵錯各種問題很有幫助。Service 類別可讓您指定日誌的輸出位置。除非使用者將輸出導向某個位置,否則日誌輸出會被忽略。

檔案輸出

若要變更記錄輸出以儲存到特定檔案

Selenium v4.10

                .withLogFile(getLogLocation())

注意:Java 也允許透過系統屬性設定檔案輸出
屬性鍵:InternetExplorerDriverService.IE_DRIVER_LOGFILE_PROPERTY
屬性值:代表日誌檔路徑的字串

    service = webdriver.IeService(log_output=log_path, log_level="INFO")

    driver = webdriver.Ie(service=service)

主控台輸出

若要變更記錄輸出以在主控台中顯示為 STDOUT

Selenium v4.10

                .withLogOutput(System.out)

注意:Java 也允許透過系統屬性設定主控台輸出;
屬性鍵:InternetExplorerDriverService.IE_DRIVER_LOGFILE_PROPERTY
屬性值:DriverService.LOG_STDOUTDriverService.LOG_STDERR

Selenium v4.11

    service = webdriver.IeService(log_output=subprocess.STDOUT)

    driver = webdriver.Ie(service=service)

記錄層級

有 6 個可用的日誌層級:FATALERRORWARNINFODEBUGTRACE。如果指定了記錄輸出,則預設層級為 FATAL

                .withLogLevel(InternetExplorerDriverLogLevel.WARN)

注意:Java 也允許透過系統屬性設定日誌層級
屬性鍵:InternetExplorerDriverService.IE_DRIVER_LOGLEVEL_PROPERTY
屬性值:InternetExplorerDriverLogLevel.DEBUG.toString() 列舉的字串表示形式

    service = webdriver.IeService(log_output=log_path, log_level="WARN")

    driver = webdriver.Ie(service=service)

Selenium v4.10

      service.args << '-log-level=WARN'

支援檔案路徑

                .withExtractPath(getTempDirectory())
**注意**:Java 也允許透過系統屬性設定日誌層級:\ 屬性鍵:`InternetExplorerDriverService.IE_DRIVER_EXTRACT_PATH_PROPERTY`\ 屬性值:代表支援檔案目錄路徑的字串

Selenium v4.11

    service = webdriver.IeService(service_args=["–extract-path=" + temp_dir])

    driver = webdriver.Ie(service=service)

Selenium v4.8

      service.args << "–extract-path=#{root_directory}"
上次修改日期:2024 年 12 月 20 日:[py] fix ie code line (#2107) (add39da6446)