Python version 3.9.1 Selenium version 4.25.0 Safari version 18.1.1
I want to operate Safari using Selenium. For this purpose, I would like to set the UserAgent to iOS and change the viewport. What should I do? The following is the content programmed with the Chrome driver. I would like to achieve this using the Safari driver.
import time,os
import chromedriver_binary
from selenium import webdriver
from selenium.webdriver import Safari
from selenium.webdriver.safari.options import Options as SafariOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome import service
# selenium 4
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()),options = chrome_options)
# iPhone 13 params
viewport = {
"width": 390,
"height": 844,
"deviceScaleFactor": 3,
"mobile": True
}
#Chrome setting
ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Mobile/15E148 Safari/604.1"
driver.execute_cdp_cmd("Emulation.setDeviceMetricsOverride", viewport)
# change user agent
driver.execute_cdp_cmd("Emulation.setUserAgentOverride", {"userAgent": ua})
# ページにアクセス
driver.get("https://hogehoge")