如何使用Selenium和Python绕过Tor网络设置确认弹出窗口

2024-05-20 22:54:35 发布

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

我正试图通过selenium自动使用Tor浏览器:

import time
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = '/Applications/TorBrowser.app/Contents/MacOS/firefox'
firefox_binary = FirefoxBinary(binary)
try:
    driver = webdriver.Firefox(firefox_binary="/Applications/Tor Browser.app/Contents/MacOS/firefox")
    driver.get("https://www.google.com/")
    time.sleep(3)
finally:
    driver.close()

我正在尝试使用Tor浏览器进行抓取,一切正常,但每次我运行代码时,您都必须手动单击“连接”按钮。如何在selenium中禁用确认屏幕或自动接受Tor browser


Tags: fromimportapptimedriverseleniumcontents浏览器
1条回答
网友
1楼 · 发布于 2024-05-20 22:54:35

浏览器配置屏幕

connect_to_tor

。。。出现,因为您尚未在代码块中显式配置或浏览器设置。因此,每次执行程序时,它都会在不使用代理设置的情况下启动新的TOR浏览上下文,并假设您是第一次运行TOR Browser。因此,您将在每次运行时看到Tor Network Settings窗口


解决方案

要删除Tor Network Settings窗口,您需要在代码块中配置Tor Network Settings,如下所示:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
    import os
    
    torexe = os.popen(r'C:\Users\Soma Bhattacharjee\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
    profile = FirefoxProfile(r'C:\Users\Soma Bhattacharjee\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
    profile.set_preference('network.proxy.type', 1)
    profile.set_preference('network.proxy.socks', '127.0.0.1')
    profile.set_preference('network.proxy.socks_port', 9050)
    profile.set_preference("network.proxy.socks_remote_dns", False)
    profile.update_preferences()
    driver = webdriver.Firefox(firefox_profile= profile, executable_path=r'C:\WebDrivers\geckodriver.exe')
    driver.get("http://check.torproject.org")
    
  • 浏览器快照:

TOR_BROWSER


参考资料

您可以在以下内容中找到一些详细的讨论:

相关问题 更多 >