如何解决使用 User-Agent Switcher 和 Spoof Timezone 中的特定设置加载 Firefox 用户配置文件
我使用的是 Linux Mint 20。我将 User-Agent Switcher and Manager 和 Spoof Timezone 与 firefox 结合使用。我想加载当前的 firefox 用户配置文件,并使用 selenium 使用 Chrome 99.0.7113.93 (Windows) 用户代理。除此之外,当右键单击欺骗时区时,有一个选项从 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 and 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()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。