Python-在private mod中使用Selenium启动firefox

2024-04-20 07:02:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下脚本:

#!/usr/bin/python3
from selenium import webdriver
import time

def getProfile():
    profile = webdriver.FirefoxProfile()
    profile.set_preference("browser.privatebrowsing.autostart", True)
    return profile

def main():
    browser = webdriver.Firefox(firefox_profile=getProfile())

    #browser shall call the URL
    browser.get("http://www.google.com")
    time.sleep(5)
    browser.quit()

if __name__ == "__main__":
    main()

如何管理Firefox以私有模式启动?


Tags: fromimportbrowser脚本bintimemainusr
1条回答
网友
1楼 · 发布于 2024-04-20 07:02:21

引用@Laas在How might I simulate a private browsing experience in Watir? (Selenium)的点:

Selenium is equivalent to turning on Private Browsing.

以及"Private Browsing"的定义:

Private Browsing allows you to browse the Internet without saving any information about which sites and pages you’ve visited.

由于每次你通过selenium webdriver启动firefox,它都会创建一个全新的匿名配置文件,你实际上是在私下浏览。


如果仍要在Firefox中强制使用私有模式,请将browser.privatebrowsing.autostart配置选项设置为true

from selenium import webdriver

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("browser.privatebrowsing.autostart", True)

driver = webdriver.Firefox(firefox_profile=firefox_profile)

另外,请参见:

相关问题 更多 >