本文的目的是介绍无法在AWS机器上的python中从Selenium中调用Firefox的详细情况,特别关注python连接aws云数据库的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为
本文的目的是介绍无法在AWS机器上的python中从Selenium中调用Firefox的详细情况,特别关注python连接aws云数据库的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解无法在AWS机器上的python中从Selenium中调用Firefox的机会,同时也不会遗漏关于mac 搭建 python+selenium+firefox 爬虫、Python + Selenium + Firefox 使用代理 auth 的用户名密码授权、Python / Selenium / Firefox:无法使用指定的配置文件路径启动Firefox、python selenium firefox使用的知识。
本文目录一览:- 无法在AWS机器上的python中从Selenium中调用Firefox(python连接aws云数据库)
- mac 搭建 python+selenium+firefox 爬虫
- Python + Selenium + Firefox 使用代理 auth 的用户名密码授权
- Python / Selenium / Firefox:无法使用指定的配置文件路径启动Firefox
- python selenium firefox使用
无法在AWS机器上的python中从Selenium中调用Firefox(python连接aws云数据库)
我正在尝试使用python中的硒来使用javascript抓取一些动态页面。但是,在pypi页面(http://pypi.python.org/pypi/selenium)上遵循硒的说明后,我无法调用firefox。我在AWS
ubuntu 12.04上安装了firefox。我收到的错误消息是:
In [1]: from selenium import webdriverIn [2]: br = webdriver.Firefox()---------------------------------------------------------------------------WebDriverException Traceback (most recent call last)/home/ubuntu/<ipython-input-2-d6a5d754ea44> in <module>()----> 1 br = webdriver.Firefox()/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.pyc in __init__(self, firefox_profile, firefox_binary, timeout) 49 RemoteWebDriver.__init__(self, 50 command_executor=ExtensionConnection("127.0.0.1", self.profile,---> 51 self.binary, timeout), 52 desired_capabilities=DesiredCapabilities.FIREFOX) 53/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.pyc in __init__(self, host, firefox_profile, firefox_binary, timeout) 45 self.profile.add_extension() 46---> 47 self.binary.launch_browser(self.profile) 48 _URL = "http://%s:%d/hub" % (HOST, PORT) 49 RemoteConnection.__init__(/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.pyc in launch_browser(self, profile) 42 43 self._start_from_profile_path(self.profile.path)---> 44 self._wait_until_connectable() 45 46 def kill(self):/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.pyc in _wait_until_connectable(self) 79 raise WebDriverException("The browser appears to have exited " 80 "before we could connect. The output was: %s" %---> 81 self._get_firefox_output()) 82 if count == 30: 83 self.kill()WebDriverException: Message: ''The browser appears to have exited before we could connect. The output was: Error: no display specified\n''
我确实在网上搜索,发现其他人也遇到了这个问题(https://groups.google.com/forum/?fromgroups=#!topic/selenium-
users/21sJrOJULZY)。但是我不理解解决方案,如果可以的话。
谁能帮我吗?谢谢!
答案1
小编典典问题是Firefox需要显示。我在示例中使用pyvirtualdisplay来模拟显示。解决方案是:
from pyvirtualdisplay import Displayfrom selenium import webdriverdisplay = Display(visible=0, size=(1024, 768))display.start()driver= webdriver.Firefox()driver.get("http://www.somewebsite.com/")<---some code--->#driver.close() # Close the current window.driver.quit() # Quit the driver and close every associated window.display.stop()
请注意,pyvirtualdisplay需要以下后端之一:Xvfb,Xephyr,Xvnc。
这样可以解决您的问题。
mac 搭建 python+selenium+firefox 爬虫
今天闲来无事看到 selenium 可以做自动化测试框架,就来试试手。
1、安装 pip
2、pip install –U selenium
完成之后就可以耍代码了,先举个栗子,测试一下:
from selenium import webdriver
import time
dr = webdriver.Firefox()
time.sleep(5)
print ''will close''
dr.quit()
print ''close''
出师不利啊,报错了:
selenium.common.exceptions.WebDriverException: Message: ''geckodriver'' executable needs to be in PATH.
原来是少了个驱动:geckodriver,链接给你,拿走不谢。
接着修改代码:
from selenium import webdriver
import time
dr = webdriver.Firefox(executable_path=''/Users/JYD/Downloads/geckodriver'')
time.sleep(5)
print ''will''
dr.quit()
print ''close''
大功告成,完美!
Python + Selenium + Firefox 使用代理 auth 的用户名密码授权
米扑代理,全球领导的代理品牌,专注代理行业近十年,提供开放、私密、独享代理,并可免费试用
米扑代理官网:https://proxy.mimvp.com
本文示例,是结合米扑代理的私密、独享、开放代理,专门研发的示例,
支持 http、https 的无密码、白名单 ip、密码授权三种类型
本博客转自米扑博客:Python + Selenium + Firefox 使用代理 auth 的用户名密码授权
示例中,用的插件 xpi 请到米扑代理官网,或米扑官方 github 下载
本文,直接给出完整的代码,都经过严格验证通过,具体请见注释
Python + Firefox + 插件(closeproxy.xpi)
其中,closeproxy.xpi 文件,需要 Google、Bing 搜下都能搜到下载地址
完整的测试代码如下:
from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary from selenium.webdriver.common.proxy import * from pyvirtualdisplay import Display from base64 import b64encode proxy = { "host": "123.57.78.100", "port": "12345", "user": "username", "pass": "password" } profile = webdriver.FirefoxProfile() # add new header profile.add_extension("modify_headers-0.7.1.1-fx.xpi") profile.set_preference("extensions.modify_headers.currentVersion", "0.7.1.1-fx") profile.set_preference("modifyheaders.config.active", True) profile.set_preference("modifyheaders.headers.count", 1) profile.set_preference("modifyheaders.headers.action0", "Add") profile.set_preference("modifyheaders.headers.name0", "Proxy-Switch-Ip") profile.set_preference("modifyheaders.headers.value0", "yes") profile.set_preference("modifyheaders.headers.enabled0", True) # add proxy profile.set_preference(''network.proxy.type'', 1) profile.set_preference(''network.proxy.http'', proxy[''host'']) profile.set_preference(''network.proxy.http_port'', int(proxy[''port''])) profile.set_preference(''network.proxy.no_proxies_on'', ''localhost, 127.0.0.1'') #profile.set_preference("network.proxy.username", ''aaaaa'') #profile.set_preference("network.proxy.password", ''bbbbb'') # Proxy auto login profile.add_extension(''closeproxy.xpi'') credentials = ''{user}:{pass}''.format(**proxy) credentials = b64encode(credentials.encode(''ascii'')).decode(''utf-8'') profile.set_preference(''extensions.closeproxyauth.authtoken'', credentials) profile.update_preferences() driver = webdriver.Firefox(profile) driver.get("https://proxy.mimvp.com/ip.php") print driver.page_source driver.quit()
米扑代理出品:完整、验证、权威的示例
#!/usr/bin/env python # -*- coding:utf-8 -*- # # Selenium + Firefox 支持 http、https # # 米扑代理示例: # https://proxy.mimvp.com/demo2.php # # 米扑代理购买: # https://proxy.mimvp.com # # mimvp.com # 2017-01-08 # Python + Selenium + Firefox 设置密码时,需要使用到两个插件: # 插件1: modify_headers-0.7.1.1-fx.xpi # 下载地址:https://github.com/mimvp/mimvp-proxy-demo # # 方式2: close_proxy_authentication-1.1.xpi # 下载地址:https://github.com/mimvp/mimvp-proxy-demo # # 本示例由米扑代理原创,测试代理来自于米扑代理 # 密码授权和白名单ip设置,请见米扑代理 - 会员中心:https://proxy.mimvp.com/usercenter/userinfo.php?p=whiteip from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary from selenium.webdriver.common.proxy import * from pyvirtualdisplay import Display # from xvfbwrapper import Xvfb import bs4, os from base64 import b64encode import sys reload(sys) sys.setdefaultencoding(''utf8'') ## webdriver + firefox (不使用代理,爬取网页) def spider_url_firefox(url): browser = None display = None try: display = Display(visible=0, size=(800, 600)) display.start() browser = webdriver.Firefox() # 打开 FireFox 浏览器 browser.get(url) content = browser.page_source print("content: " + str(content)) finally: if browser: browser.quit() if display: display.stop() ## webdriver + firefox + proxy + whiteip (无密码,或白名单ip授权) ## 米扑代理:https://proxy.mimvp.com def spider_url_firefox_by_whiteip(url): browser = None display = None ## 白名单ip,请见米扑代理会员中心: https://proxy.mimvp.com/usercenter/userinfo.php?p=whiteip mimvp_proxy = { ''ip'' : ''140.143.62.84'', # ip ''port_https'' : 19480, # http, https ''port_socks'' : 19481, # socks5 ''username'' : ''mimvp-user'', ''password'' : ''mimvp-pass'' } try: display = Display(visible=0, size=(800, 600)) display.start() profile = webdriver.FirefoxProfile() # add proxy profile.set_preference(''network.proxy.type'', 1) # ProxyType.MANUAL = 1 if url.startswith("http://"): profile.set_preference(''network.proxy.http'', mimvp_proxy[''ip'']) profile.set_preference(''network.proxy.http_port'', mimvp_proxy[''port_https'']) # 访问http网站 elif url.startswith("https://"): profile.set_preference(''network.proxy.ssl'', mimvp_proxy[''ip'']) profile.set_preference(''network.proxy.ssl_port'', mimvp_proxy[''port_https'']) # 访问https网站 else: profile.set_preference(''network.proxy.socks'', mimvp_proxy[''ip'']) profile.set_preference(''network.proxy.socks_port'', mimvp_proxy[''port_socks'']) profile.set_preference(''network.proxy.ftp'', mimvp_proxy[''ip'']) profile.set_preference(''network.proxy.ftp_port'', mimvp_proxy[''port_https'']) profile.set_preference(''network.proxy.no_proxies_on'', ''localhost,127.0.0.1'') ## 不存在此用法,不能这么设置用户名密码 (舍弃) # profile.set_preference("network.proxy.username", ''mimvp-guest'') # profile.set_preference("network.proxy.password", ''welcome2mimvp'') profile.update_preferences() browser = webdriver.Firefox(profile) # 打开 FireFox 浏览器 browser.get(url) content = browser.page_source print("content: " + str(content)) finally: if browser: browser.quit() if display: display.stop() ## webdriver + firefox + proxy + https (https密码授权) ## 米扑代理:https://proxy.mimvp.com def spider_url_firefox_by_proxy(url): browser = None display = None ## 授权密码,请见米扑代理会员中心: https://proxy.mimvp.com/usercenter/userinfo.php?p=whiteip mimvp_proxy = { ''ip'' : ''140.143.62.84'', # ip ''port_https'' : 19480, # http, https ''port_socks'' : 19481, # socks5 ''username'' : ''mimvp-user'', ''password'' : ''mimvp-pass'' } try: display = Display(visible=0, size=(800, 600)) display.start() profile = webdriver.FirefoxProfile() # add new header profile.add_extension("modify_headers-0.7.1.1-fx.xpi") profile.set_preference("extensions.modify_headers.currentVersion", "0.7.1.1-fx") profile.set_preference("modifyheaders.config.active", True) profile.set_preference("modifyheaders.headers.count", 1) profile.set_preference("modifyheaders.headers.action0", "Add") profile.set_preference("modifyheaders.headers.name0", "Proxy-Switch-Ip") profile.set_preference("modifyheaders.headers.value0", "yes") profile.set_preference("modifyheaders.headers.enabled0", True) # add proxy profile.set_preference(''network.proxy.type'', 1) # ProxyType.MANUAL = 1 if url.startswith("http://"): profile.set_preference(''network.proxy.http'', mimvp_proxy[''ip'']) profile.set_preference(''network.proxy.http_port'', mimvp_proxy[''port_https'']) # 访问http网站 elif url.startswith("https://"): profile.set_preference(''network.proxy.ssl'', mimvp_proxy[''ip'']) profile.set_preference(''network.proxy.ssl_port'', mimvp_proxy[''port_https'']) # 访问https网站 # Proxy auto login (自动填写密码,进行代理授权) profile.add_extension(''close_proxy_authentication-1.1.xpi'') credentials = ''{username}:{password}''.format(username=mimvp_proxy[''username''], password=mimvp_proxy[''password'']) # auth credentials = b64encode(credentials.encode(''ascii'')).decode(''utf-8'') profile.set_preference(''extensions.closeproxyauth.authtoken'', credentials) profile.update_preferences() browser = webdriver.Firefox(profile) # 打开 FireFox 浏览器 browser.get(url) content = browser.page_source print("content: " + str(content)) finally: if browser: browser.quit() if display: display.stop() ## webdriver + firefox + proxy + socks (socks密码授权) ## 米扑代理:https://proxy.mimvp.com def spider_url_firefox_by_socks(url): browser = None display = None ## 授权密码,请见米扑代理会员中心: https://proxy.mimvp.com/usercenter/userinfo.php?p=whiteip mimvp_proxy = { ''ip'' : ''140.143.62.84'', # ip ''port_https'' : 19480, # http, https ''port_socks'' : 19481, # socks5 ''username'' : ''mimvp-user'', ''password'' : ''mimvp-pass'' } proxy_config = Proxy({ ''proxyType'' : ProxyType.MANUAL, # 1 ''httpProxy'' : mimvp_proxy[''ip''] + ":" + str(mimvp_proxy[''port_https'']), ''sslProxy'' : mimvp_proxy[''ip''] + ":" + str(mimvp_proxy[''port_https'']), ''socksProxy'' : mimvp_proxy[''ip''] + ":" + str(mimvp_proxy[''port_socks'']), ''ftpProxy'' : mimvp_proxy[''ip''] + ":" + str(mimvp_proxy[''port_https'']), ''noProxy'' : ''localhost,127.0.0.1'', ''socksUsername'' : mimvp_proxy[''username''], ''socksPassword'' : mimvp_proxy[''password''], }) try: display = Display(visible=0, size=(800, 600)) display.start() profile = webdriver.FirefoxProfile() # add new header profile.add_extension("modify_headers-0.7.1.1-fx.xpi") profile.set_preference("extensions.modify_headers.currentVersion", "0.7.1.1-fx") profile.set_preference("modifyheaders.config.active", True) profile.set_preference("modifyheaders.headers.count", 1) profile.set_preference("modifyheaders.headers.action0", "Add") profile.set_preference("modifyheaders.headers.name0", "Proxy-Switch-Ip") profile.set_preference("modifyheaders.headers.value0", "yes") profile.set_preference("modifyheaders.headers.enabled0", True) # auto save auth profile.set_preference("signon.autologin.proxy", ''true'') profile.set_preference("network.websocket.enabled", ''false'') profile.set_preference(''network.proxy.share_proxy_settings'', ''false'') profile.set_preference(''network.automatic-ntlm-auth.allow-proxies'', ''false'') profile.set_preference(''network.auth.use-sspi'', ''false'') profile.update_preferences() browser = webdriver.Firefox(proxy=proxy_config, firefox_profile=profile) # 打开 FireFox 浏览器 browser.get(url) content = browser.page_source print("content: " + str(content)) finally: if browser: browser.quit() if display: display.stop() if __name__ == ''__main__'': url = ''https://ip.cn'' url = ''https://mimvp.com'' url = ''https://proxy.mimvp.com/ip.php'' # 不使用代理,爬取网页,成功 spider_url_firefox(url) # 代理无密码,或设置白名单ip,成功 spider_url_firefox_by_whiteip(url) # http, https 密码授权,成功 spider_url_firefox_by_proxy(url) # socks5 密码授权,失败 (仍然是本机ip请求的,不是代理ip请求) spider_url_firefox_by_socks(url)
参考推荐:
WebDriver 配置 Firefox 代理服务器
Python + Selenium + Chrome 使用代理 auth 的用户名密码授权
Selenium Webdriver 以代理 proxy 方式启动 firefox,ie,chrome (米扑博客)
selenium/py/selenium/webdriver/firefox/firefox_profile.py (github 官网)
Python + Selenium + Firefox 如何使用需要 auth 的代理 (SegmentFault)
How to set proxy authentication (user & password) using python selenium (Stack Overflow)
Python Selenium Webdriver - Proxy Authentication (Stack Overflow)
Selenium2——profile 设置、启动 Firefox 浏览器 (CSDN)
Selenium 中配置 FirefoxProfile 控制文件下载路径、SSL 和 Proxy
Selenium + Chrome Diver 使用带用户名密码认证的 HTTP 代理的方法
chromedriver 代理设置(账号密码)
Python / Selenium / Firefox:无法使用指定的配置文件路径启动Firefox
如何解决Python / Selenium / Firefox:无法使用指定的配置文件路径启动Firefox?
我花了大约2个小时(是的,我太慢了)猜测为什么不起作用。我发现了为什么个人资料不保存回去。
当然,传递给FirefoxProfile("myprofile/full/path")
它的配置文件是在运行时使用的,但是它并没有保存回去,因为(也许不是很明显)硒用于测试,并且测试应该在没有缓存,没有任何配置文件,尽可能干净的情况下运行。
配置文件功能(可能)是为了允许在运行测试之前安装某些扩展名和设置而创建的,而不是为了保存它们。
诀窍是打印出来print driver.firefox_profile.path
。它与通常的确有所不同,其名称为 不仅仅是 ,因此请阅读您应该猜测的配置文件。
现在,剩下的唯一事情就是找回它:)
运行此脚本,安装内容,编辑内容,然后再次运行;):
#!/usr/bin/env python
#! -*- coding: utf-8 -*-
import selenium
from selenium import webdriver
import os, sys, time
# 1- set profile
profile = os.path.dirname(sys.argv[0]) + "/selenita"
fp = webdriver.FirefoxProfile(profile)
driver = webdriver.Firefox(firefox_profile=fp)
# 2- get tmp file location
profiletmp = driver.firefox_profile.path
# but... the current profile is a copy of the original profile :/
print "running profile " + profiletmp
driver.get("http://httpbin.org")
time.sleep(2)
raw_input("Press a key when finish doing things") # I''ve installed an extension
# 3- then save back
print "saving profile " + profiletmp + " to " + profile
if os.system("cp -R " + profiletmp + "/* " + profile ):
print "files should be copied :/"
driver.quit()
sys.exit(0)
您可以遵循该架构,也可以根据需要简单地编辑FirefoxProfilea。
解决方法
我尝试使用指定的配置文件启动Firefox:
firefox_profile = webdriver.FirefoxProfile(''/Users/p2mbot/projects/test/firefox_profile'')
driver = webdriver.Firefox(firefox_profile=firefox_profile)
driver.get(''http://google.com'')
time.sleep(60)
driver.quit()
/Users/p2mbot/projects/test/firefox_profile
-这个目录是正确的Firefox配置文件目录,我用
firefox-bin --ProfileManager
但是当我通过selenium检查firefox中的about:cache页面时,它具有不同的缓存路径:
Storage disk location: /var/folders/jj/rdpd1ww53n95y5vx8w618k3h0000gq/T/tmpp2ahq70_/webdriver-py-profilecopy/cache2
如果通过firefox-bin –ProfileManager运行firefox并选择配置文件,它将显示在about:cache页面正确路径中
/Users/p2mbot/projects/test/firefox_profile
为什么WebDriver忽略了Firefox的配置文件路径?使用铬不会有这样的问题。
python selenium firefox使用
演示的版本信息如下:
Python 3.6.0
Selenium 3.5.0
Firefox 55.0.3
geckodriver v1.0.18.0 win64
1、前提准备
2、Python安装selenium





3、 下载安装geckodriver


4、访问页面

5、PyCharm配置Selenium


6、运行代码
关于无法在AWS机器上的python中从Selenium中调用Firefox和python连接aws云数据库的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于mac 搭建 python+selenium+firefox 爬虫、Python + Selenium + Firefox 使用代理 auth 的用户名密码授权、Python / Selenium / Firefox:无法使用指定的配置文件路径启动Firefox、python selenium firefox使用等相关知识的信息别忘了在本站进行查找喔。
本文标签: