Selenium Python 中的“无法解码来自木偶的响应”错误

问题描述 投票:0回答:1

我尝试将 Tor 与 Selenium 和 Python 一起使用。

但我偶然发现了这个错误:“selenium.common.exceptions.WebDriverException:消息:无法解码来自木偶的响应

这是代码:

from selenium.webdriver import Firefox  
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options

profile_path = "C:\\Users\\alest\\Documents\\Tor\\Browser\\TorBrowser\\Data\\Browser\\profile.default"

firefox_options=Options()
firefox_options.set_preference('profile', profile_path)
firefox_options.set_preference('network.proxy.type', 1)
firefox_options.set_preference('network.proxy.socks', '127.0.0.1')
firefox_options.set_preference('network.proxy.socks_port', 9050)
firefox_options.set_preference("network.proxy.socks_remote_dns", False)
firefox_options.binary_location = "C:\\Users\\alest\\Documents\\Tor\\Browser\\firefox.exe"

service = Service("C:\\Users\\alest\\Desktop\\my_project\\B_0_T\\gecko\\geckodriver.exe")

driver = webdriver.Firefox(service=service, options=firefox_options)

driver.get("https://www.amazon.com/")

我使用的是 Windows 10、Python 3.11.1 64 位、Selenium 4.15.2、Geckodriver 0.33.0 64 位、Tor 102.10.0 64 位

我已经在网上和 Stackoverflow 上搜索过这个问题,但我找到的所有解决方案都是关于人们在服务器上运行脚本时遇到这个问题以及内存问题的。

python selenium-webdriver geckodriver tor firefox-marionette
1个回答
0
投票

您遇到的错误,

"selenium.common.exceptions.WebDriverException: Message: Failed to decode response from marionette,"
通常与 Selenium、GeckoDriver 和 Firefox 版本之间的不匹配有关。

更新 Firefox、更新 GeckoDriver、更新 Selenium

pip install --upgrade selenium

检查 Firefox 二进制路径 确保您指定的

binary_location
指向正确的 Firefox 二进制文件。 Tor 浏览器使用 Firefox 的修改版本,因此使用正确的二进制文件至关重要。验证路径是否正确。

firefox_options.binary_location = "C:\\Users\\alest\\Documents\\Tor\\Browser\\firefox.exe"

确保您使用的 Tor Browser 版本与您安装的 Firefox 和 GeckoDriver 版本兼容。

**Additional Configuration**

将以下首选项添加到 Firefox 选项中:

firefox_options.set_preference("marionette.enabled", True)
firefox_options.set_preference("browser.link.open_newwindow", 3)

在创建 WebDriver 实例之前添加这些行。 进行这些调整后,尝试再次运行脚本

© www.soinside.com 2019 - 2024. All rights reserved.