【问题标题】:Python Selenium binding with TOR browserPython Selenium 与 TOR 浏览器绑定
【发布时间】:2016-03-22 21:01:26
【问题描述】:

我对此进行了研究,但得到了解决方案:

from selenium import webdriver
profile = webdriver.FirefoxProfile()
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)
driver = webdriver.Firefox(profile)
driver.get('http://estoeslapollaconcebol.la')

它给出了错误:

无法加载配置文件。简介目录: C:\Users\HPPAV1~1\AppData\Local\Temp\tmppcuwx3xd Firefox 输出: 无

当我尝试那个解决方案时。

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
profile=webdriver.FirefoxProfile('C:\\Users\\HP PAV 15\\Desktop\\Tor     Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default\\')
binary =FirefoxBinary('C:\\Users\\HP PAV 15\\Desktop\\Tor Browser\\Browser\\firefox')
#browser = binary.launch_browser(profile)
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9150)
browser=webdriver.Firefox( binary, profile)
browser.get("http://yahoo.com")
browser.save_screenshot("/Users/admin/Pictures/screenshot.png")
browser.close()

它给了我以下错误:

Traceback(最近一次调用最后一次):文件 “C:/Python34/torfirstscript.py”,第 10 行,在 browser=webdriver.Firefox(binary, profile) 文件 "C:\Python34\lib\site-packages\selenium-2.43.0-py3.4.egg\selenium\webdriver\firefox\webdriver.py", 第 46 行,在 init 中 self.NATIVE_EVENTS_ALLOWED 和 self.profile.native_events_enabled) AttributeError: 'FirefoxBinary' 对象没有属性 'native_events_enabled'

通过申请

browser=webdriver.Firefox( firefox_binary = binary, firefox_profile = profile)

我收到了这个错误:

Traceback(最近一次调用最后一次): 文件“C:\Python34\torfirstscript.py”,第 9 行,在 浏览器=webdriver.Firefox(firefox_binary = 二进制,firefox_profile = >profile) init 中的文件“C:\Python34\lib\site-packages\selenium-2.43.0->py3.4.egg\selenium\webdriver\firefox\webdriver.py”,第 59 行 self.binary,超时), 文件“C:\Python34\lib\site-packages\selenium-2.43.0->py3.4.egg\selenium\webdriver\firefox\extension_connection.py”,第 47 行,在 >init self.binary.launch_browser(self.profile) 文件“C:\Python34\lib\site-packages\selenium-2.43.0->py3.4.egg\selenium\webdriver\firefox\firefox_binary.py”,第 64 行,在 launch_browser self._wait_until_connectable() _wait_until_connectable 中的文件“C:\Python34\lib\site-packages\selenium-2.43.0-py3.4.egg\selenium\webdriver\firefox\firefox_binary.py”,第 108 行 self.profile.path,self._get_firefox_output())) selenium.common.exceptions.WebDriverException:消息:“无法加载配置文件。配置文件目录:>C:\Users\HPPAV1~1\AppData\Local\Temp\tmpig7zvx_0\webdriver-py-profilecopy Firefox 输出:无”

将该图像作为输出。

【问题讨论】:

    标签: python selenium selenium-webdriver tor


    【解决方案1】:

    使用以下方法更新硒:

    pip install -U selenium
    

    然后运行您的代码,当然是在启动 TOR 之后。 已确认并修复此错误。

    P.S:如果您使用的是 Linux,请不要忘记“Sudo”。

    【讨论】:

    • 第一个代码不正确,应该交换配置文件和二进制文件。
    • 是的,你是对的,很抱歉我没有仔细看。
    • 你不需要在 Linux 上使用 sudo 运行!
    【解决方案2】:

    在 Windows 上使用 Selenium 和 Tor 的工作示例:

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    
    binary = FirefoxBinary(r"C:\Program Files (x86)\TorBrowser\Browser\firefox.exe")
    profile = FirefoxProfile(r"C:\Program Files (x86)\TorBrowser\Browser\TorBrowser\Data\Browser\profile.default")
    
    driver = webdriver.Firefox(profile, binary)
    driver.get("http://stackoverflow.com")
    driver.save_screenshot("screenshot.png")
    driver.quit()
    

    【讨论】:

    • 这在 Windows 上对我不起作用。 tor浏览器打开,过了一会儿,我得到selenium.common.exceptions.WebDriverException
    【解决方案3】:

    在 Windows 上安装最新 TOR 的代码:

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    
    binary = FirefoxBinary(r"C:\Users\<Windows User>\Desktop\Tor Browser\Browser\firefox.exe")
    profile = FirefoxProfile(r"C:\Users\<Windows User>\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default")
    
    driver = webdriver.Firefox(profile, binary)
    driver.get("http://stackoverflow.com")
    

    【讨论】:

      【解决方案4】:

      我在 Windows 上解决了我的类似问题:

      from selenium import webdriver
      from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
      from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
      
      binary = FirefoxBinary(r"C:\Users\<Windows User>\Desktop\Tor Browser\Browser\firefox.exe")
      
      driver = webdriver.Firefox(firefox_binary=binary)
      driver.profile.set_preference('network.proxy.type', 1)
      driver.profile.set_preference('network.proxy.socks', '127.0.0.1')
      driver.profile.set_preference('network.proxy.socks_port', 9051)
      
      driver.get("http://stackoverflow.com")
      

      【讨论】:

        【解决方案5】:

        我尝试了这样的方法,并且成功了:

        profile = webdriver.FirefoxProfile()
        profile.set_preference('network.proxy.type', 1)
        profile.set_preference('network.proxy.socks', '127.0.0.1')
        profile.set_preference('network.proxy.socks_port', 9150)
        driver = webdriver.Firefox(profile)
        

        在执行此操作时打开 Tor 浏览器

        【讨论】:

          【解决方案6】:

          由于其中一些方法在当前 Windows 版本中不起作用,因此返回“tor failed to start”错误会通知用户,为了启动代理,他们需要在执行脚本之前已经运行了 tor。

          【讨论】:

            【解决方案7】:

            另一个简单的解决方案是: 在 Firefox 或 Chrome 中创建新的配置文件, 将浏览器配置为使用 Tor 代理(将 SOCKS 5 代理设置为地址 127.0.0.1 端口 9150),然后在使用 webdriver 时加载该配置文件。

            【讨论】:

              【解决方案8】:

              此功能自 2020 年 5 月 12 日起生效。在运行此脚本之前,您需要运行 Tor 浏览器。这将在 Chrome 中运行 Tor。只会在隐身模式下这样做。如果您删除该选项,它将通过您的 isp 连接。

              import time
              from selenium import webdriver
              from selenium.webdriver.chrome.options import Options
              
              
              tor_proxy = "127.0.0.1:9150"
              
              chrome_options = Options()
              
              '''chrome_options.add_argument("--test-type")'''
              chrome_options.add_argument('--ignore-certificate-errors')
              '''chrome_options.add_argument('--disable-extensions')'''
              chrome_options.add_argument('disable-infobars')
              '''chrome_options.add_argument("--incognito")'''
              chrome_options.add_argument('--user-data=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Default')
              chrome_options.add_argument('--proxy-server=socks5://%s' % tor_proxy)
              driver = webdriver.Chrome(executable_path='C:\\chromedriver.exe', options=chrome_options)
              driver.get('https://www.google.com')
              time.sleep(4)
              driver.switch_to.frame(0)
              driver.find_element_by_id("introAgreeButton").click()
              

              【讨论】:

                【解决方案9】:
                from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
                from selenium import webdriver
                
                caps = DesiredCapabilities.FIREFOX
                caps['proxy'] = {
                    'proxyType': 'MANUAL',
                    'socksProxy': '127.0.0.1:9050',
                    'socksVersion': 5
                }
                
                driver = webdriver.Firefox(executable_path=r"C:\webdrivers\geckodriver.exe", capabilities=caps)
                

                在我的例子中,这段代码是唯一有效的。

                【讨论】:

                • 请在您的回答中提供更多详细信息。正如目前所写的那样,很难理解您的解决方案。
                【解决方案10】:

                这对我有用,这不使用tor浏览器而是geckodriver

                pip install selenium webdriver-manager

                import asyncio
                import os
                import subprocess
                
                from selenium.webdriver import Firefox
                from selenium.webdriver.firefox.options import Options
                from selenium.webdriver.firefox.service import Service
                from webdriver_manager.firefox import GeckoDriverManager
                
                profile_path = os.path.expandvars(
                    r"%USERPROFILE%\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default"
                )
                options = Options()
                options.set_preference("profile", profile_path)
                service = Service(
                    # os.path.expandvars(r"%USERPROFILE%\Desktop\Tor Browser\Browser\firefox.exe"),
                    executable_path=GeckoDriverManager().install()
                )
                
                options.set_preference("network.proxy.type", 1)
                options.set_preference("network.proxy.socks", "127.0.0.1")
                options.set_preference("network.proxy.socks_port", 9050)
                options.set_preference("network.proxy.socks_remote_dns", False)
                
                
                async def main():
                    async def cleanup():
                        driver.quit()
                        print(torexe.pid)
                        torexe.kill()
                
                    try:
                        # https://stackoverflow.com/a/62686067/8608146
                        torexe = subprocess.Popen(
                            os.path.expandvars(
                                r"%USERPROFILE%\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe"
                            )
                        )
                        driver = Firefox(service=service, options=options)
                        driver.get("https://check.torproject.org")
                        driver.save_screenshot("screenshot.png")
                    except Exception as e:
                        print(e, type(e))
                    finally:
                        await cleanup()
                
                
                if __name__ == "__main__":
                    asyncio.run(main())
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-10-31
                  • 2020-04-07
                  • 2017-01-01
                  • 2020-08-02
                  • 1970-01-01
                  • 2018-08-24
                  • 1970-01-01
                  相关资源
                  最近更新 更多