我正在尝试在 Linux 计算机上使用本地代理 (Tor) 将 RSelenium 与 Firefox 结合使用。
我在 this tuto 之后安装 Tor 没有任何问题,以及命令行 wget -qO - https://api.ipify.org; echo
给我一个新的IP。
现在我愿意使用 Firefox 和 RSelenium,通过端口 9050 上的 Tor 本地主机:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9050 *:*
LISTEN 0 128 127.0.0.1:9051 *:*
我使用独立的 selenium java (selenium-server-standalone-2.53.0.jar),它可以与常规 RSelenium 配合使用:下面是一个在 ipchicken 上显示 IP 的示例。
library(RSelenium)
remDr <- remoteDriver(
remoteServerAddr = "localhost",
port = 4444L,
browserName = "firefox"
)
remDr$open()
remDr$navigate("https://ipchicken.com/")
ip <- remDr$findElements(using = "css", value ='b')
print(ip[[1]]$getElementText())
我确实得到了我的 IP。现在我希望看到 Tor 实现这一目标。因此,我尝试在使用 Firefox 连接远程驱动程序时添加代理选项:
eCaps <- list("moz:firefoxOptions" = list(
args = c('--proxy-server=localhost:9050'
)))
remDr <- remoteDriver(
remoteServerAddr = "localhost",
port = 4444L,
browserName = "firefox",
extraCapabilities = eCaps
)
我尝试了'--proxy-server=localhost:9050'
、'--proxy-server=http://localhost:9050'
、 '--proxy-server=socks5://localhost:9050'
, '--proxy-server=127.0.0.1:9050'
,它没有输出任何错误并给出我的初始IP。所以它不起作用。独立版表示它确实使用以下选项执行:例如
22:59:10.288 INFO - Executing: [new session: Capabilities [{nativeEvents=true, browserName=firefox, javascriptEnabled=true, moz:firefoxOptions={args=--proxy-server= 127.0.0.1:9050}, version=, platform=ANY}]])
22:59:10.297 INFO - Creating a new session for Capabilities [{nativeEvents=true, browserName=firefox, javascriptEnabled=true, moz:firefoxOptions={args=--proxy-server= 127.0.0.1:9050}, version=, platform=ANY}]
22:59:30.323 INFO - Done: [new session: Capabilities [{nativeEvents=true, browserName=firefox, javascriptEnabled=true, moz:firefoxOptions={args=--proxy-server= 127.0.0.1:9050}, version=, platform=ANY}]]
我做错了什么?
编辑
在用户1207289的回答之后,意识到你可以直接create a firefox profile在RSelenium
中,我尝试过:
eCaps <- makeFirefoxProfile(list(network.proxy.type = 1,
network.proxy.socks = "127.0.0.1",
network.proxy.socks_port = 9050,
network.proxy.socks_version = 5))
remDr <- remoteDriver(
remoteServerAddr = "localhost",
port = 4444L,
browserName = "firefox",
extraCapabilities = eCaps
)
我使用整数作为 network.proxy.socks_port
、network.proxy.socks_port
和 network.proxy.type
因为 this question ,但也尝试过性格,但没有成功。我尝试过使用和不使用network.proxy.socks_version = 5
,但它不起作用(我得到了正常的IP)。我尝试了 network.proxy.socks_port = 9150
,但没有成功。
我也尝试过
eCaps <- list("moz:firefoxOptions" = list(
args = c('network.proxy.socks=127.0.0.1:9050' ,'network.proxy.type=1' )
)
)
但这也不起作用。
最佳答案
我可以使用 webdriver 和 firefox 连接到 TOR,如下所示。只需确保 TOR 已安装并运行即可。我在 mac(catalina)上使用过它。您可以根据您的操作系统检查端口设置,以防它们不同。
它是用 C# 编写的,但您几乎可以对任何绑定(bind)执行此操作
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.SetPreference("network.proxy.type", 1);
firefoxOptions.SetPreference("network.proxy.socks", "127.0.0.1");
firefoxOptions.SetPreference("network.proxy.socks_port", 9150);
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService();
IWebDriver driver = new FirefoxDriver(service, firefoxOptions);
当这打开一个firefox浏览器实例时,只需访问https://check.torproject.org/在同一实例上检查它是否连接到 TOR。这将确认您已连接并显示您的新 IP
关于RSelenium:配置 Firefox Remotedriver 以使用 Tor 网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62074583/