Edge 特定功能

這些是 Microsoft Edge 瀏覽器特有的功能和特色。

Microsoft Edge 是使用 Chromium 實作的,最早支援的版本為 v79。與 Chrome 類似,edgedriver 的主要版本號必須與 Edge 瀏覽器的主要版本號相符。

選項

所有瀏覽器通用的功能已在選項頁面中說明。

Chromium 特有的功能記錄在 Google 的功能與 ChromeOptions頁面中

使用基本定義選項啟動 Edge 會話看起來像這樣

    EdgeOptions options = new EdgeOptions();
    driver = new EdgeDriver(options);
    options = webdriver.EdgeOptions()
    driver = webdriver.Edge(options=options)
            var options = new EdgeOptions();
            driver = new EdgeDriver(options);
      options = Selenium::WebDriver::Options.edge
      @driver = Selenium::WebDriver.for :edge, options: options
    let options = new edge.Options();
    driver = new Builder()
      .forBrowser(Browser.EDGE)
      .setEdgeOptions(options)
      .build();

參數

args 參數用於啟動瀏覽器時要使用的一系列命令列開關。有兩個絕佳資源可供研究這些參數

常用的 args 包括 --start-maximized--headless=new--user-data-dir=...

在選項中新增一個參數

    options.addArguments("--start-maximized");
    options.add_argument("--start-maximized")
            options.AddArgument("--start-maximized");
      options.args << '--start-maximized'
      .setEdgeOptions(options.addArguments('--headless=new'))

在指定位置啟動瀏覽器

binary 參數接受要使用之替代瀏覽器位置的路徑。使用此參數,您可以使用 chromedriver 來驅動各種基於 Chromium 的瀏覽器。

在選項中新增瀏覽器位置

    options.setBinary(getEdgeLocation());
    options.binary_location = edge_bin
            options.BinaryLocation = GetEdgeLocation();
      options.binary = edge_location

新增擴充功能

extensions 參數接受 crx 檔案。至於未封裝的目錄,請改用 load-extension 參數,如這篇文章中所述。

在選項中新增擴充功能

    options.addExtensions(extensionFilePath);
    options.add_extension(extension_file_path)
            options.AddExtension(extensionFilePath);
      options.add_extension(extension_file_path)
      .setEdgeOptions(options.addExtensions(['./test/resources/extensions/webextensions-selenium-example.crx']))

保持瀏覽器開啟

detach 參數設定為 true 將在程序結束後保持瀏覽器開啟,只要未將 quit 命令傳送至驅動程式即可。

注意:這已經是 Java 中的預設行為。

    options.add_experimental_option("detach", True)

注意:這已經是 .NET 中的預設行為。

      options.detach = true
      .setEdgeOptions(options.detachDriver(true))

排除參數

MSEdgedriver 有幾個預設參數用於啟動瀏覽器。如果您不想要新增這些參數,請將它們傳遞到 excludeSwitches 中。一個常見的範例是重新開啟彈出視窗封鎖程式。可以從Chromium 原始碼中剖析預設參數的完整列表

在選項中設定排除的參數

    options.setExperimentalOption("excludeSwitches", List.of("disable-popup-blocking"));
    options.add_experimental_option('excludeSwitches', ['disable-popup-blocking'])
            options.AddExcludedArgument("disable-popup-blocking");
      options.exclude_switches << 'disable-popup-blocking'
      .setEdgeOptions(options.excludeSwitches('enable-automation'))

服務

關於建立預設 Service 物件,以及設定驅動程式位置和埠的範例,可以在驅動程式服務頁面中找到。

記錄輸出

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

檔案輸出

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

Selenium v4.10

    EdgeDriverService service = new EdgeDriverService.Builder().withLogFile(logLocation).build();

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

    service = webdriver.EdgeService(log_output=log_path)
            service.LogPath = GetLogLocation();

主控台輸出

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

Selenium v4.10

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

注意:Java 也允許透過系統屬性設定主控台輸出;
屬性金鑰:EdgeDriverService.EDGE_DRIVER_LOG_PROPERTY
屬性值:DriverService.LOG_STDOUTDriverService.LOG_STDERR

Selenium v4.11

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

$stdout$stderr 都是有效值

Selenium v4.10

      service.log = $stdout

記錄層級

有 6 個可用的記錄層級:ALLDEBUGINFOWARNINGSEVEREOFF。請注意,--verbose 等同於 --log-level=ALL,而 --silent 等同於 --log-level=OFF,因此此範例僅以通用方式設定記錄層級

Selenium v4.8

    EdgeDriverService service =
        new EdgeDriverService.Builder().withLoglevel(ChromiumDriverLogLevel.DEBUG).build();

注意:Java 也允許透過系統屬性設定記錄層級
屬性金鑰:EdgeDriverService.EDGE_DRIVER_LOG_LEVEL_PROPERTY
屬性值:ChromiumDriverLogLevel 列舉的字串表示

    service = webdriver.EdgeService(service_args=['--log-level=DEBUG'], log_output=log_path)

Selenium v4.10

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

記錄檔功能

只有在記錄到檔案時,才有 2 個功能可用

  • 附加日誌
  • 可讀的時間戳記

若要使用它們,您也需要明確指定日誌路徑和日誌層級。日誌輸出將由驅動程式而非程序管理,因此可能會看到細微差異。

Selenium v4.8

    EdgeDriverService service =
        new EdgeDriverService.Builder().withAppendLog(true).withReadableTimestamp(true).build();

注意:Java 也允許透過系統屬性切換這些功能
屬性金鑰:EdgeDriverService.EDGE_DRIVER_APPEND_LOG_PROPERTYEdgeDriverService.EDGE_DRIVER_READABLE_TIMESTAMP
屬性值:"true""false"

    service = webdriver.EdgeService(service_args=['--append-log', '--readable-timestamp'], log_output=log_path)

Selenium v4.8

      service.args << '--append-log'
      service.args << '--readable-timestamp'

停用組建檢查

Edge 瀏覽器和 msedgedriver 版本應該相符,如果不符,驅動程式將會出錯。如果您停用組建檢查,您可以強制驅動程式與任何版本的 Edge 一起使用。請注意,這是不受支援的功能,且不會調查錯誤。

Selenium v4.8

    EdgeDriverService service =
        new EdgeDriverService.Builder().withBuildCheckDisabled(true).build();

注意:Java 也允許透過系統屬性停用組建檢查
屬性金鑰:EdgeDriverService.EDGE_DRIVER_DISABLE_BUILD_CHECK
屬性值:"true""false"

    service = webdriver.EdgeService(service_args=['--disable-build-check'], log_output=log_path)
            service.DisableBuildCheck = true;

Selenium v4.8

      service.args << '--disable-build-check'

Internet Explorer 模式

Microsoft Edge 可以在「Internet Explorer 相容性模式」下驅動,該模式將 Internet Explorer Driver 類別與 Microsoft Edge 結合使用。請閱讀Internet Explorer 頁面以取得更多詳細資訊。

特殊功能

某些瀏覽器已實作它們獨有的其他功能。

投放

您可以使用 Edge 驅動 Chrome Cast 裝置,包括共享分頁

    List<Map<String, String>> sinks = driver.getCastSinks();
    if (!sinks.isEmpty()) {
      String sinkName = sinks.get(0).get("name");
      driver.startTabMirroring(sinkName);
      driver.stopCasting(sinkName);
    }
        sinks = driver.get_sinks()
        if sinks:
            sink_name = sinks[0]['name']
            driver.start_tab_mirroring(sink_name)
            driver.stop_casting(sink_name)
      sinks = @driver.cast_sinks
      unless sinks.empty?
        device_name = sinks.first['name']
        @driver.start_cast_tab_mirroring(device_name)
        expect { @driver.stop_casting(device_name) }.not_to raise_exception

網路條件

您可以模擬各種網路條件。


    ChromiumNetworkConditions networkConditions = new ChromiumNetworkConditions();
    networkConditions.setOffline(false);
    networkConditions.setLatency(java.time.Duration.ofMillis(20)); // 20 ms of latency
    networkConditions.setDownloadThroughput(2000 * 1024 / 8); // 2000 kbps
    networkConditions.setUploadThroughput(2000 * 1024 / 8);   // 2000 kbps
    network_conditions = {
        "offline": False,
        "latency": 20,  # 20 ms of latency
        "download_throughput": 2000 * 1024 / 8,  # 2000 kbps
        "upload_throughput": 2000 * 1024 / 8,    # 2000 kbps
    }
    driver.set_network_conditions(**network_conditions)
      @driver.network_conditions = {offline: false, latency: 100, throughput: 200}

日誌

    LogEntries logs = driver.manage().logs().get(LogType.BROWSER);
    logs = driver.get_log("browser")
      logs = @driver.logs.get(:browser)

權限

    driver.setPermission("camera", "denied");
    driver.set_permissions('camera', 'denied')
      @driver.add_permission('camera', 'denied')
      @driver.add_permissions('clipboard-read' => 'denied', 'clipboard-write' => 'prompt')

DevTools

請參閱Chrome DevTools章節,以取得有關在 Edge 中使用 DevTools 的更多資訊