Safari 特定功能
这些是特定于Apple Safari浏览器的功能和特性.
与Chromium和Firefox驱动不同, safari驱动随操作系统安装. 要在 Safari 上启用自动化, 请从终端运行以下命令:
safaridriver --enable
选项
所有浏览器通用的Capabilities在选项页.
Safari独有的Capabilities可以在Apple的页面关于Safari的WebDriver 上找到
使用基本定义的选项启动 Safari 会话如下所示:
        SafariOptions options = new SafariOptions();
        driver = new SafariDriver(options);/examples/java/src/test/java/dev/selenium/browsers/SafariTest.java
package dev.selenium.browsers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariDriverService;
import org.openqa.selenium.safari.SafariOptions;
@EnabledOnOs(OS.MAC)
public class SafariTest {
    public SafariDriver driver;
    @AfterEach
    public void quit() {
        if (driver != null) {
            driver.quit();
        }
    }
    @Test
    public void basicOptions() {
        SafariOptions options = new SafariOptions();
        driver = new SafariDriver(options);
    }
    @Test
    public void enableLogs() {
        SafariDriverService service = new SafariDriverService.Builder()
                .withLogging(true)
                .build();
        driver = new SafariDriver(service);
    }
    
    public void safariTechnologyPreview() {
        SafariOptions options = new SafariOptions();
        options.setUseTechnologyPreview(true);
        driver = new SafariDriver(options);
    }
}
    options = webdriver.SafariOptions()
    driver = webdriver.Safari(options=options)/examples/python/tests/browsers/test_safari.py
import sys
import pytest
from selenium import webdriver
@pytest.mark.skipif(sys.platform != "darwin", reason="requires Mac")
def test_basic_options():
    options = webdriver.SafariOptions()
    driver = webdriver.Safari(options=options)
    driver.quit()
@pytest.mark.skipif(sys.platform != "darwin", reason="requires Mac")
def test_enable_logging():
    service = webdriver.SafariService(enable_logging=True)
    driver = webdriver.Safari(service=service)
    driver.quit()
@pytest.mark.skip(reason="Not installed on Mac GitHub Actions Runner Image")
def test_technology_preview():
    options = webdriver.SafariOptions()
    options.use_technology_preview = True
    service = webdriver.SafariService(
        executable_path='/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver'
    )
    driver = webdriver.Safari(options=options, service=service)
    driver.quit()
            var options = new SafariOptions();
            driver = new SafariDriver(options);/examples/dotnet/SeleniumDocs/Browsers/SafariTest.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Safari;
using SeleniumDocs.TestSupport;
namespace SeleniumDocs.Browsers
{
    [TestClassCustom]
    [EnabledOnOs("OSX")]
    public class SafariTest
    {
        private SafariDriver driver;
        [TestCleanup]
        public void QuitDriver()
        {
            driver.Quit();
        }
        [TestMethod]
        public void BasicOptions()
        {
            var options = new SafariOptions();
            driver = new SafariDriver(options);
        }
    }
}    it 'basic options' do
      options = Selenium::WebDriver::Options.safari/examples/ruby/spec/browsers/safari_spec.rb
# frozen_string_literal: true
require 'spec_helper'
# rubocop:disable RSpec/MultipleDescribes
RSpec.describe 'Safari', exclusive: {platform: :macosx} do
  describe 'Options' do
    it 'basic options' do
      options = Selenium::WebDriver::Options.safari
      @driver = Selenium::WebDriver.for :safari, options: options
    end
  end
  describe 'Service' do
    let(:directory) { "#{Dir.home}/Library/Logs/com.apple.WebDriver/*" }
    it 'enables logs' do
      original_count = Dir[directory].length
      service = Selenium::WebDriver::Service.safari
      service.args << '--diagnose'
      @driver = Selenium::WebDriver.for :safari, service: service
      expect(Dir[directory].length - original_count).to eq 1
    end
    it 'does not set log output' do
      service = Selenium::WebDriver::Service.safari
      expect {
        service.log = $stdout
      }.to raise_error(Selenium::WebDriver::Error::WebDriverError, /Safari Service does not support setting log output/)
    end
  end
end
RSpec.describe 'Safari Technology Preview', skip: 'This test is being skipped as GitHub Actions ' \
                                                  'have no support for Safari Technology Preview' do
  it 'sets the technology preview' do
    Selenium::WebDriver::Safari.technology_preview!
    local_driver = Selenium::WebDriver.for :safari
    expect(local_driver.capabilities.browser_name).to eq 'Safari Technology Preview'
  end
end
# rubocop:enable RSpec/MultipleDescribes
    let driver = new Builder()
      .forBrowser(Browser.SAFARI)
      .setSafariOptions(options)
      .build();/examples/javascript/test/browser/safariSpecificCap.spec.js
const safari = require('selenium-webdriver/safari');
const {Browser, Builder} = require("selenium-webdriver");
const options = new safari.Options();
const process  =  require('node:process');
describe('Should be able to Test Command line arguments', function () {
  (process.platform === 'darwin' ? it : it.skip)('safari caps', async function () {
    let driver = new Builder()
      .forBrowser(Browser.SAFARI)
      .setSafariOptions(options)
      .build();
    await driver.get('https://www.selenium.dev/selenium/web/blank.html');
    await driver.quit();
  });
});
val options = SafariOptions()
val driver = SafariDriver(options)移动端
那些希望在iOS上自动化Safari的人可以参考 Appium 项目.
服务
所有浏览器通用的服务设置在 服务页面.
日志
与其他浏览器不同,
Safari 浏览器不允许您选择日志的输出位置或更改级别.
一个可用选项是关闭或打开日志.
如果日志处于打开状态,
则可以在以下位置找到它们: ~/Library/Logs/com.apple.WebDriver/.
                .withLogging(true)/examples/java/src/test/java/dev/selenium/browsers/SafariTest.java
package dev.selenium.browsers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariDriverService;
import org.openqa.selenium.safari.SafariOptions;
@EnabledOnOs(OS.MAC)
public class SafariTest {
    public SafariDriver driver;
    @AfterEach
    public void quit() {
        if (driver != null) {
            driver.quit();
        }
    }
    @Test
    public void basicOptions() {
        SafariOptions options = new SafariOptions();
        driver = new SafariDriver(options);
    }
    @Test
    public void enableLogs() {
        SafariDriverService service = new SafariDriverService.Builder()
                .withLogging(true)
                .build();
        driver = new SafariDriver(service);
    }
    
    public void safariTechnologyPreview() {
        SafariOptions options = new SafariOptions();
        options.setUseTechnologyPreview(true);
        driver = new SafariDriver(options);
    }
}
注意: Java也允许使用环境变量进行设置;
属性键: SafariDriverService.SAFARI_DRIVER_LOGGING
属性值: "true" 或 "false"
    service = webdriver.SafariService(enable_logging=True)/examples/python/tests/browsers/test_safari.py
import sys
import pytest
from selenium import webdriver
@pytest.mark.skipif(sys.platform != "darwin", reason="requires Mac")
def test_basic_options():
    options = webdriver.SafariOptions()
    driver = webdriver.Safari(options=options)
    driver.quit()
@pytest.mark.skipif(sys.platform != "darwin", reason="requires Mac")
def test_enable_logging():
    service = webdriver.SafariService(enable_logging=True)
    driver = webdriver.Safari(service=service)
    driver.quit()
@pytest.mark.skip(reason="Not installed on Mac GitHub Actions Runner Image")
def test_technology_preview():
    options = webdriver.SafariOptions()
    options.use_technology_preview = True
    service = webdriver.SafariService(
        executable_path='/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver'
    )
    driver = webdriver.Safari(options=options, service=service)
    driver.quit()
/examples/ruby/spec/browsers/safari_spec.rb
# frozen_string_literal: true
require 'spec_helper'
# rubocop:disable RSpec/MultipleDescribes
RSpec.describe 'Safari', exclusive: {platform: :macosx} do
  describe 'Options' do
    it 'basic options' do
      options = Selenium::WebDriver::Options.safari
      @driver = Selenium::WebDriver.for :safari, options: options
    end
  end
  describe 'Service' do
    let(:directory) { "#{Dir.home}/Library/Logs/com.apple.WebDriver/*" }
    it 'enables logs' do
      original_count = Dir[directory].length
      service = Selenium::WebDriver::Service.safari
      service.args << '--diagnose'
      @driver = Selenium::WebDriver.for :safari, service: service
      expect(Dir[directory].length - original_count).to eq 1
    end
    it 'does not set log output' do
      service = Selenium::WebDriver::Service.safari
      expect {
        service.log = $stdout
      }.to raise_error(Selenium::WebDriver::Error::WebDriverError, /Safari Service does not support setting log output/)
    end
  end
end
RSpec.describe 'Safari Technology Preview', skip: 'This test is being skipped as GitHub Actions ' \
                                                  'have no support for Safari Technology Preview' do
  it 'sets the technology preview' do
    Selenium::WebDriver::Safari.technology_preview!
    local_driver = Selenium::WebDriver.for :safari
    expect(local_driver.capabilities.browser_name).to eq 'Safari Technology Preview'
  end
end
# rubocop:enable RSpec/MultipleDescribes
Safari Technology Preview
Apple 提供了其浏览器的开发版本 — Safari Technology Preview
在代码中使用此版本:
        options.setUseTechnologyPreview(true);
        driver = new SafariDriver(options);/examples/java/src/test/java/dev/selenium/browsers/SafariTest.java
package dev.selenium.browsers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariDriverService;
import org.openqa.selenium.safari.SafariOptions;
@EnabledOnOs(OS.MAC)
public class SafariTest {
    public SafariDriver driver;
    @AfterEach
    public void quit() {
        if (driver != null) {
            driver.quit();
        }
    }
    @Test
    public void basicOptions() {
        SafariOptions options = new SafariOptions();
        driver = new SafariDriver(options);
    }
    @Test
    public void enableLogs() {
        SafariDriverService service = new SafariDriverService.Builder()
                .withLogging(true)
                .build();
        driver = new SafariDriver(service);
    }
    
    public void safariTechnologyPreview() {
        SafariOptions options = new SafariOptions();
        options.setUseTechnologyPreview(true);
        driver = new SafariDriver(options);
    }
}
    options = webdriver.SafariOptions()
    options.use_technology_preview = True
    service = webdriver.SafariService(
        executable_path='/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver'
    )
    driver = webdriver.Safari(options=options, service=service)/examples/python/tests/browsers/test_safari.py
import sys
import pytest
from selenium import webdriver
@pytest.mark.skipif(sys.platform != "darwin", reason="requires Mac")
def test_basic_options():
    options = webdriver.SafariOptions()
    driver = webdriver.Safari(options=options)
    driver.quit()
@pytest.mark.skipif(sys.platform != "darwin", reason="requires Mac")
def test_enable_logging():
    service = webdriver.SafariService(enable_logging=True)
    driver = webdriver.Safari(service=service)
    driver.quit()
@pytest.mark.skip(reason="Not installed on Mac GitHub Actions Runner Image")
def test_technology_preview():
    options = webdriver.SafariOptions()
    options.use_technology_preview = True
    service = webdriver.SafariService(
        executable_path='/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver'
    )
    driver = webdriver.Safari(options=options, service=service)
    driver.quit()
                                                  'have no support for Safari Technology Preview' do
  it 'sets the technology preview' do/examples/ruby/spec/browsers/safari_spec.rb
# frozen_string_literal: true
require 'spec_helper'
# rubocop:disable RSpec/MultipleDescribes
RSpec.describe 'Safari', exclusive: {platform: :macosx} do
  describe 'Options' do
    it 'basic options' do
      options = Selenium::WebDriver::Options.safari
      @driver = Selenium::WebDriver.for :safari, options: options
    end
  end
  describe 'Service' do
    let(:directory) { "#{Dir.home}/Library/Logs/com.apple.WebDriver/*" }
    it 'enables logs' do
      original_count = Dir[directory].length
      service = Selenium::WebDriver::Service.safari
      service.args << '--diagnose'
      @driver = Selenium::WebDriver.for :safari, service: service
      expect(Dir[directory].length - original_count).to eq 1
    end
    it 'does not set log output' do
      service = Selenium::WebDriver::Service.safari
      expect {
        service.log = $stdout
      }.to raise_error(Selenium::WebDriver::Error::WebDriverError, /Safari Service does not support setting log output/)
    end
  end
end
RSpec.describe 'Safari Technology Preview', skip: 'This test is being skipped as GitHub Actions ' \
                                                  'have no support for Safari Technology Preview' do
  it 'sets the technology preview' do
    Selenium::WebDriver::Safari.technology_preview!
    local_driver = Selenium::WebDriver.for :safari
    expect(local_driver.capabilities.browser_name).to eq 'Safari Technology Preview'
  end
end
# rubocop:enable RSpec/MultipleDescribes
最后修改 July 29, 2025: Adding trailing / to retrieve code from GitHub (48b97e9bf8)




