[英]FirefoxProfile with private mode for Selenium
I'm tying to create multiple windows of one website, so I need new identity for each.我想为一个网站创建多个 windows,所以我需要为每个网站创建新的身份。 Private mode would be nice solution for me, I think.
我认为私人模式对我来说是个不错的解决方案。 But old ways to do it doesn't give result:
但是旧的方法并没有给出结果:
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("browser.privatebrowsing.autostart", True)
browser = webdriver.Firefox(firefox_profile=firefox_profile)
def main():
browser.switch_to.new_window('window')
browser.get("https://example.com")
I couldn't find any information in docks, so maybe you can help我在码头找不到任何信息,所以也许你可以帮忙
As per Selenium 4 beta 1 release notes:根据Selenium 4 beta 1发行说明:
Deprecate all but
Options
andService
arguments in driver instantiation.在驱动程序实例化中弃用除
Options
和Service
arguments 之外的所有内容。 (#9125,#9128)(#9125,#9128)
So you will see an error as:所以你会看到一个错误:
firefox_profile has been deprecated, please pass in an Options object
You have to use an instance of Options
to pass the FirefoxProfile preferences as follows:您必须使用
Options
的实例来传递 FirefoxProfile 首选项,如下所示:
from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.chrome.service import Service
def main():
firefox_options = Options()
firefox_options.set_preference("browser.privatebrowsing.autostart", True)
s = Service('C:\\BrowserDrivers\\geckodriver.exe')
driver = webdriver.Firefox(service=s, options=firefox_options)
driver.get("https://www.google.com")
if __name__== "__main__" :
main()
Browser Snapshot:浏览器快照:
You can find a couple of relevant detailed discussions in:您可以在以下位置找到一些相关的详细讨论:
This should be the "new" way:这应该是“新”方式:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
service = Service(r"C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=service, options=chrome_options)
It should work the same way with Firefox.它应该与 Firefox 以相同的方式工作。
I figured how to make private mode for firefox:我想出了如何为 firefox 创建私有模式:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
def main():
firefox_options = Options()
firefox_options.add_argument('-private')
driver = webdriver.Firefox(options=firefox_options)
# driver.get("https://example.com")
if __name__ == "__main__":
main()
I've commented line with get to make sure that browser truly opens in private mode.我已经评论了 get 以确保浏览器真正以私有模式打开。 You can see it in tab name.
您可以在选项卡名称中看到它。
But it didn't gave me new identity for each new window as I expected.但它并没有像我预期的那样为每个新的 window 提供新的身份。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.