簡體   English   中英

FirefoxProfile 與 Selenium 的私有模式

[英]FirefoxProfile with private mode for Selenium

我想為一個網站創建多個 windows,所以我需要為每個網站創建新的身份。 我認為私人模式對我來說是個不錯的解決方案。 但是舊的方法並沒有給出結果:

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")

我在碼頭找不到任何信息,所以也許你可以幫忙

根據Selenium 4 beta 1發行說明:

在驅動程序實例化中棄用除OptionsService arguments 之外的所有內容。 (#9125,#9128)

所以你會看到一個錯誤:

firefox_profile has been deprecated, please pass in an Options object

您必須使用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()

瀏覽器快照:

FirefoxProfile_Options


參考

您可以在以下位置找到一些相關的詳細討論:

這應該是“新”方式:

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)

它應該與 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()

我已經評論了 get 以確保瀏覽器真正以私有模式打開。 您可以在選項卡名稱中看到它。

但它並沒有像我預期的那樣為每個新的 window 提供新的身份。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM