Python 用硒打开浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15316304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open tor browser with selenium
提问by user1907403
Is it possible to make selenium use the TOR browser? Does anyone have any code they could copy-paste?
是否可以让 selenium 使用 TOR 浏览器?有没有人有任何可以复制粘贴的代码?
回答by rdsoze
Using ruby,
使用红宝石,
profile = Selenium::WebDriver::Firefox::Profile.new
profile.proxy = Selenium::WebDriver::Proxy.new :socks => '127.0.0.1:9050' #port where TOR runs
browser = Watir::Browser.new :firefox, :profile => profile
To confirm that you are using Tor, use https://check.torproject.org/
要确认您正在使用 Tor,请使用https://check.torproject.org/
回答by Willem van Ketwich
I looked into this, and unless I'm mistaken, on face value it's not possible.
我调查了这个,除非我弄错了,否则从表面上看这是不可能的。
The reason this cannot be done is because:
不能这样做的原因是:
- Tor Browser is based on the Firefox code.
- Tor Browser has specific patches to the Firefox code to prevent external applications communicating with the Tor Browser (including blocking the Components.Interfaces library).
- The Selenium Firefox WebDriver communicates with the browser through Javascript libraries that are, as aforementioned, blocked by Tor Browser.
- Tor 浏览器基于 Firefox 代码。
- Tor 浏览器对 Firefox 代码有特定的补丁,以防止外部应用程序与 Tor 浏览器通信(包括阻止 Components.Interfaces 库)。
- Selenium Firefox WebDriver 通过 Javascript 库与浏览器通信,如前所述,这些库被 Tor 浏览器阻止。
This is presumably so no-one outside of the Tor Browser either on your box or over the internet knows about your browsing.
这大概是因为 Tor 浏览器之外的人在你的盒子上或通过互联网知道你的浏览。
Your alternatives are:
您的选择是:
- Use a Tor proxy through Firefox instead of the Tor Browser (see the link in the comments of the question).
- Rebuild the Firefox source code with the Tor Browser patches excluding those that prevent external communication with Tor Browser.
- 通过 Firefox 使用 Tor 代理而不是 Tor 浏览器(请参阅问题评论中的链接)。
- 使用 Tor 浏览器补丁重建 Firefox 源代码,排除那些阻止与 Tor 浏览器进行外部通信的补丁。
I suggest the former.
我建议前者。
回答by user2426679
Don't use the TBB, just set the correct proxy settings in whatever browser you're using. In FF for example, like this:
不要使用 TBB,只需在您使用的任何浏览器中设置正确的代理设置。以FF为例,像这样:
#set some privacy settings
ff_prof.set_preference( "places.history.enabled", False )
ff_prof.set_preference( "privacy.clearOnShutdown.offlineApps", True )
ff_prof.set_preference( "privacy.clearOnShutdown.passwords", True )
ff_prof.set_preference( "privacy.clearOnShutdown.siteSettings", True )
ff_prof.set_preference( "privacy.sanitize.sanitizeOnShutdown", True )
ff_prof.set_preference( "signon.rememberSignons", False )
ff_prof.set_preference( "network.cookie.lifetimePolicy", 2 )
ff_prof.set_preference( "network.dns.disablePrefetch", True )
ff_prof.set_preference( "network.http.sendRefererHeader", 0 )
#set socks proxy
ff_prof.set_preference( "network.proxy.type", 1 )
ff_prof.set_preference( "network.proxy.socks_version", 5 )
ff_prof.set_preference( "network.proxy.socks", '127.0.0.1' )
ff_prof.set_preference( "network.proxy.socks_port", 9050 )
ff_prof.set_preference( "network.proxy.socks_remote_dns", True )
#if you're really hardcore about your security
#js can be used to reveal your true i.p.
ff_prof.set_preference( "javascript.enabled", False )
#get a huge speed increase by not downloading images
ff_prof.set_preference( "permissions.default.image", 2 )
##
# programmatically start tor (in windows environment)
##
tor_path = "C:\this\is\the\location\of\" #tor.exe
torrc_path = "C:\you\need\to\create\this\file\torrc"
DETACHED_PROCESS = 0x00000008
#calling as a detached_process means the program will not die with your python program - you will need to manually kill it
##
# somebody please let me know if there's a way to make this a child process that automatically dies (in windows)
##
tor_process = subprocess.Popen( '"' + tor_path+'tor.exe" --nt-service "-f" "' + torrc_path + '"', creationflags=DETACHED_PROCESS )
#attach to tor controller
## imports ##
# import stem.socket
# import stem.connection
# import stem.Signal
##
tor_controller = stem.socket.ControlPort( port=9051 )
control_password = 'password'
#in your torrc, you need to store the hashed version of 'password' which you can get with: subprocess.call( '"' + tor_path+'tor.exe" --hash-password %s' %control_password )
stem.connection.authenticate( tor_controller, password=control_password )
#check that everything is good with your tor_process by checking bootstrap status
tor_controller.send( 'GETINFO status/bootstrap-phase' )
response = worker.tor_controller.recv()
response = response.content()
#I will leave handling of response status to you
回答by Chirag Shetty
//just check your tor browser's port number and change that accordingly in the //code
// 只需检查您的 Tor 浏览器的端口号并在 // 代码中相应地更改它
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', 9150)
browser=webdriver.Firefox(profile)
browser.get("http://yahoo.com")
browser.save_screenshot("screenshot.png")
browser.close()
回答by dmmfll
Yes, it is possible to make selenium use the TOR browser.
是的,可以让 selenium 使用 TOR 浏览器。
I was able to do so on both Ubuntu and Mac OS X.
我能够在 Ubuntu 和 Mac OS X 上这样做。
Two things have to happen:
有两件事必须发生:
Set the binary path to the firefox binary that Tor uses. On a Mac this path would typically be
/Applications/TorBrowser.app/Contents/MacOS/firefox. On my Ubuntu machine it is/usr/bin/tor-browser/Browser/firefox.The Tor browser uses a SOCKS host at 127.0.0.1:9150 either through Vidalia or Tor installation. Launch Tor once from the Finder and leave it open so that Vidalia will be running. The instances launched with selenium will use the SOCKS host that Vidalia starts, too.
将二进制路径设置为 Tor 使用的 firefox 二进制文件。在 Mac 上,此路径通常为
/Applications/TorBrowser.app/Contents/MacOS/firefox. 在我的 Ubuntu 机器上,它是/usr/bin/tor-browser/Browser/firefox.Tor 浏览器通过 Vidalia 或 Tor 安装使用位于 127.0.0.1:9150 的 SOCKS 主机。从 Finder 启动 Tor 一次并保持打开状态,以便 Vidalia 运行。使用 selenium 启动的实例也将使用 Vidalia 启动的 SOCKS 主机。
Here is the code to accomplish those two things. I run this on Mac OS X Yosemite:
这是完成这两件事的代码。我在 Mac OS X Yosemite 上运行这个:
import os
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium import webdriver
# path to the firefox binary inside the Tor package
binary = '/Applications/TorBrowser.app/Contents/MacOS/firefox'
if os.path.exists(binary) is False:
raise ValueError("The binary path to Tor firefox does not exist.")
firefox_binary = FirefoxBinary(binary)
browser = None
def get_browser(binary=None):
global browser
# only one instance of a browser opens, remove global for multiple instances
if not browser:
browser = webdriver.Firefox(firefox_binary=binary)
return browser
if __name__ == "__main__":
browser = get_browser(binary=firefox_binary)
urls = (
('tor browser check', 'https://check.torproject.org/'),
('ip checker', 'http://icanhazip.com')
)
for url_name, url in urls:
print "getting", url_name, "at", url
browser.get(url)
On an Ubuntu system I was able to run the Tor browser via selenium. This machine has tor running at port 9051 and privoxy http proxy that uses tor at port 8118. In order for the Tor browser to pass the tor check page I had to set the http proxy to privoxy.
在 Ubuntu 系统上,我能够通过 selenium 运行 Tor 浏览器。这台机器有在 9051 端口运行的 Tor 和在 8118 端口使用 Tor 的 privoxy http 代理。为了让 Tor 浏览器通过 Tor 检查页面,我必须将 http 代理设置为 privoxy。
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium import webdriver
browser = None
proxy_address = "127.0.0.1:8118"
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': proxy_address,
})
tor = '/usr/bin/tor-browser/Browser/firefox'
firefox_binary = FirefoxBinary(tor)
urls = (
('tor_browser_check', 'https://check.torproject.org/'),
('icanhazip', 'http://icanhazip.com'),
)
keys, _ = zip(*urls)
urls_map = dict(urls)
def get_browser(binary=None, proxy=None):
global browser
if not browser:
browser = webdriver.Firefox(firefox_binary=binary, proxy=proxy)
return browser
if __name__ == "__main__":
browser = get_browser(binary=firefox_binary, proxy=proxy)
for resource in keys:
browser.get(urls_map.get(resource))
回答by serv-inc
As a newer alternative to Selenium, which only controls Firefox, have a look at Marionette. To use with the Tor Browser, enable marionette at startup via
作为仅控制 Firefox 的 Selenium 的更新替代品,请查看Marionette。要与 Tor 浏览器一起使用,请在启动时通过以下方式启用牵线木偶
Browser/firefox -marionette
(inside the bundle). Then, you can connect via
(在捆绑包内)。然后,您可以通过连接
from marionette import Marionette
client = Marionette('localhost', port=2828);
client.start_session()
and load a new page for example via
并加载一个新页面,例如通过
url='http://mozilla.org'
client.navigate(url);
For more examples, there is a tutorial.
有关更多示例,有一个教程。
Older answer
较旧的答案
The Tor project has a selenium testfor its browser. It works like:
Tor 项目对其浏览器进行了selenium 测试。它的工作原理如下:
from selenium import webdriver
ffbinary = webdriver.firefox.firefox_binary.FirefoxBinary(firefox_path=os.environ['TBB_BIN'])
ffprofile = webdriver.firefox.firefox_profile.FirefoxProfile(profile_directory=os.environ['TBB_PROFILE'])
self.driver = webdriver.Firefox(firefox_binary=ffbinary, firefox_profile=ffprofile)
self.driver.implicitly_wait(30)
self.base_url = "about:tor"
self.verificationErrors = []
self.accept_next_alert = True
self.driver.get("http://check.torproject.org/")
self.assertEqual("Congratulations. This browser is configured to use Tor.", driver.find_element_by_css_selector("h1.on").text)
As you see, this uses the environment variables TBB_BINand TBB_PROFILEfor the browser bundle and profile. You might be able to hardcode these in your code.
如您所见,这使用了环境变量TBB_BIN以及TBB_PROFILE浏览器包和配置文件。您可以在代码中对这些进行硬编码。
回答by Will D.
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
#path to TOR binary
binary = FirefoxBinary(r'...\Tor Browser\Browser\firefox.exe')
#path to TOR profile
profile = FirefoxProfile(r'...\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
driver = webdriver.Firefox(firefox_profile= profile, firefox_binary= binary)
driver.get("http://icanhazip.com")
driver.save_screenshot("screenshot.png")
driver.quit()
Using Python 3.5.1 on Windows 10
在 Windows 10 上使用 Python 3.5.1
回答by user3816638
A lot of answers are towards the right direction but this is exactly what worked for me:
很多答案都朝着正确的方向发展,但这正是对我有用的方法:
On Ubuntu:
在 Ubuntu 上:
You need to install Tor using apt command or other method, but not the binary version.
您需要使用 apt 命令或其他方法安装 Tor,但不需要使用二进制版本。
Installation guide:
安装指南:
https://linuxconfig.org/how-to-install-tor-browser-in-ubuntu-18-04-bionic-beaver-linux
https://linuxconfig.org/how-to-install-tor-browser-in-ubuntu-18-04-bionic-beaver-linux
Inside the sample.py you might need to:
在 sample.py 中,您可能需要:
- set profile of the Firefox to
torrcwhich is located most of the times in/etc/tor/. - set the binary to the Firefox binary of Tor, since Tor is just a series of configurations built atop of Firefox.
- 设置
torrc大部分时间位于Firefox 的配置文件/etc/tor/。 - 将二进制文件设置为 Tor 的 Firefox 二进制文件,因为 Tor 只是在 Firefox 之上构建的一系列配置。
You also need the geckodriver to automate firefox with selenium:
您还需要 geckodriver 使用 selenium 自动化 firefox:
- https://github.com/mozilla/geckodriver/releases(works with 0.21.0)
- Extract
chmod +x geckodriverexport PATH=$PATH:/path-to-extracted-file/geckodriver
- https://github.com/mozilla/geckodriver/releases(适用于 0.21.0)
- 提炼
chmod +x geckodriverexport PATH=$PATH:/path-to-extracted-file/geckodriver
Pay attension to the:
注意以下事项:
- "network.proxy.socks_port" = 9150
- Inside torrc ControlPort 9050, CookieAuthentication 1
- Open TorBrowser
sudo lsof -i -P -n | grep LISTENthe LISTEN port of the tor network must be the same in the script- Run the python script, while TorBrowser is open
- “network.proxy.socks_port”= 9150
- 内部 torrc ControlPort 9050, CookieAuthentication 1
- 打开 TorBrowser
sudo lsof -i -P -n | grep LISTEN脚本中 tor 网络的 LISTEN 端口必须相同- 运行 python 脚本,同时TorBrowser 打开
Thanks user2426679 https://stackoverflow.com/a/21836296/3816638for the settings.
感谢 user2426679 https://stackoverflow.com/a/21836296/3816638的设置。
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.firefox.options import Options
import subprocess
import os
profileTor = '/etc/tor/' # torrc
binary = os.path.expanduser("~/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/firefox")
firefox_binary = FirefoxBinary(binary)
firefox_profile = FirefoxProfile(profileTor)
#set some privacy settings
firefox_profile.set_preference( "places.history.enabled", False )
firefox_profile.set_preference( "privacy.clearOnShutdown.offlineApps", True )
firefox_profile.set_preference( "privacy.clearOnShutdown.passwords", True )
firefox_profile.set_preference( "privacy.clearOnShutdown.siteSettings", True )
firefox_profile.set_preference( "privacy.sanitize.sanitizeOnShutdown", True )
firefox_profile.set_preference( "signon.rememberSignons", False )
firefox_profile.set_preference( "network.cookie.lifetimePolicy", 2 )
firefox_profile.set_preference( "network.dns.disablePrefetch", True )
firefox_profile.set_preference( "network.http.sendRefererHeader", 0 )
#set socks proxy
firefox_profile.set_preference( "network.proxy.type", 1 )
firefox_profile.set_preference( "network.proxy.socks_version", 5 )
firefox_profile.set_preference( "network.proxy.socks", '127.0.0.1' )
firefox_profile.set_preference( "network.proxy.socks_port", 9150 )
firefox_profile.set_preference( "network.proxy.socks_remote_dns", True )
#if you're really hardcore about your security
#js can be used to reveal your true i.p.
firefox_profile.set_preference( "javascript.enabled", False )
#get a huge speed increase by not downloading images
firefox_profile.set_preference( "permissions.default.image", 2 )
options = Options()
options.set_headless(headless=False)
driver = webdriver.Firefox(firefox_profile=firefox_profile,firefox_options=options)
print(driver)
driver.get("https://check.torproject.org/")
driver.save_screenshot("screenshot.png")
回答by Anuj Bansal
System.setProperty("webdriver.firefox.marionette", "D:\Lib\geckodriver.exe");
String torPath = "C:\Users\HP\Desktop\Tor Browser\Browser\firefox.exe";
String profilePath = "C:\Users\HP\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default";
File torProfileDir = new File(profilePath);
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
FirefoxOptions options = new FirefoxOptions();
options.setBinary(binary);
options.setProfile(torProfile);
options.setCapability(FirefoxOptions.FIREFOX_OPTIONS,options);
WebDriver driver = new FirefoxDriver(options);

