使用用户代理切换器和欺骗时区中的特定设置加载 Firefox 用户配置文件

Load Firefox user profile with particular settings in User-Agent Switcher and Spoof Timezone

我正在使用 Linux Mint 20。我正在使用 User-Agent Switcher and Manager and Spoof Timezone 和 firefox。我想加载当前的 firefox 用户配置文件并使用 Chrome 99.0.7113.93 (Windows) 使用 selenium 的用户代理。除此之外,当右键单击 Spoof Timezone 时,有一个选项 Update timezone from IP,我也想在通过之前单击它剩下的过程。

目前我正在关注 save document.cookie output in a file 并达到:

driver = webdriver.Firefox(
    executable_path=GeckoDriverManager(cache_valid_range=1).install())

driver.get('https://www.skillshare.com/')

cookie = driver.execute_script('return document.cookie')

f = open("/home/blueray/Desktop/cookie.txt", "w")
f.write(cookie)
f.close()

driver.close()

我该怎么做?

不幸的是,您不能准确地点击带有 selenium 的扩展,因为它们不是页面的一部分 DOM。

对于 User-Agent Switcher 和 Manager 你可以直接注入用户代理而不使用扩展

对于Spoof Timezone,您可以访问about:addons,点击扩展,偏好设置,勾选根据我的IP地址自动更新时区并点击保存。不能用 selenium 来做,因为那部分在不显示这些设置的 shadowroot 下。希望当 selenium 启动时,您将在手动完成此步骤后保存设置。

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile


options = webdriver.FirefoxOptions()


# Define and set the user agent to Chrome 99;
# The User-Agent Switcher and Manager extension has no config page that we can access as an url and click on it with selenium;
# Therefore we can injecting the user agent instead
user_agent = "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.7113.93 Safari/537.36"
options.set_preference("general.useragent.override", user_agent)

# load the firefox profile 
# to get this: open in firefox the url about:profiles and its the Profile: default-release => Root Directory
firefox_profile = FirefoxProfile('/home/art/.mozilla/firefox/kd5i4tgp.default-release')

options.profile= firefox_profile

firefox_profile.add_extension("/home/art/.mozilla/firefox/kd5i4tgp.default-release/extensions/{55f61747-c3d3-4425-97f9-dfc19a0be23c}.xpi") # for spoof timezone

#download from here https://github.com/mozilla/geckodriver/releases/tag/v0.29.1 linux64.tar.gz, I've put mine in Documents
driver = webdriver.Firefox(options=options,executable_path="/home/art/Documents/geckodriver")


driver.get('https://www.skillshare.com/')

cookie = driver.execute_script('return document.cookie')

f = open("/home/blueray/Desktop/cookie.txt", "w")
f.write(cookie)
f.close()

driver.close()