简体   繁体   English

FirefoxProfile 与 Selenium 的私有模式

[英]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 and Service arguments in driver instantiation.在驱动程序实例化中弃用除OptionsService 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:浏览器快照:

FirefoxProfile_Options


References参考

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.

相关问题 Selenium:FirefoxProfile 失败,未找到异常 - Selenium: FirefoxProfile fails with not found exception Python/Selenium 隐身/私有模式 - Python/Selenium incognito/private mode selenium没有在FirefoxProfile中设置downloaddir - selenium doesn't set downloaddir in FirefoxProfile Selenium:FirefoxProfile 异常无法加载配置文件 - Selenium: FirefoxProfile exception Can't load the profile 如何通过Selenium和Python加载现有的FirefoxProfile - How to load a existing FirefoxProfile through Selenium and Python 使用 Selenium Python 在私有模式下打开 Internet Explorer - Opening Internet Explorer in Private Mode with Selenium Python Selenium 得到响应码 429 但 firefox 私有模式没有 - Selenium gets response code of 429 but firefox private mode does not 消息:错误:轮询更改失败:在通过 Selenium 和 FirefoxProfile 下载文件时尝试获取资源时出现网络错误 - Message: Error: Polling for changes failed: NetworkError when attempting to fetch resource while downloading file through Selenium and FirefoxProfile 如何使用 FirefoxProfile 或 FirefoxOptions 通过 Selenium 设置 Firefox 浏览器的窗口位置 - How to set window position of Firefox browser through Selenium using FirefoxProfile or FirefoxOptions 如何使用 Selenium Python 3.x 在私有模式下打开 Microsoft Edge (Chromium)? - How to open Microsoft Edge (Chromium) in private mode with Selenium Python 3.x?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM