Hen*_*ryM 8 python cron selenium-firefoxdriver selenium-webdriver pyvirtualdisplay
我有一个使用无头浏览器的脚本,我正在使用它crontab -e.它在前几次运行正常,然后使用以下Traceback崩溃:
Traceback (most recent call last):
File "/home/clint-selenium-firefox.py", line 83, in <module>
driver.get(url)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 248, in get
self.execute(Command.GET, {'url': url})
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Failed to decode response from marionette
Run Code Online (Sandbox Code Playgroud)
我的crontab行是:
*/10 * * * * export DISPLAY=:0 && python /home/clint-selenium-firefox.py >> /home/error.log 2>&1
Run Code Online (Sandbox Code Playgroud)
我不想用python脚本重载这个,所以我已经拿出了我认为的相关位.
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
...
driver = webdriver.Firefox()
driver.get(url)
...
driver.quit()
...
display.stop()
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助.
编辑
版本:Firefox 49.0.2; 硒:3.0.1; geckodriver:geckodriver-v0.11.1-linux64.tar.gz
代码错误(失败driver.get(url)):
driver = webdriver.Firefox()
if DEBUG: print "Opened Firefox"
for u in urls:
list_of_rows = []
list_of_old_rows = []
# get the old version of the site data
mycsvfile = u[1]
try:
with open(mycsvfile, 'r') as csvfile:
old_data = csv.reader(csvfile, delimiter=' ', quotechar='|')
for o in old_data:
list_of_old_rows.append(o)
except: pass
# get the new data
url = u[0]
if DEBUG: print url
driver.get(url)
if DEBUG: print driver.title
time.sleep(1)
page_source = driver.page_source
soup = bs4.BeautifulSoup(page_source,'html.parser')
Run Code Online (Sandbox Code Playgroud)
来自多个Firefox实例失败的NS_ERROR_SOCKET_ADDRESS_IN_USE#99 这是因为没有--marionette-port选项传递给geckodriver - 这意味着geckodriver的所有实例都会启动firefox传递相同的所需默认端口(2828).第一个firefox实例绑定到该端口,未来的实例不能和所有geckodriver实例最终连接到第一个firefox实例 - 这会产生各种不可预测的行为.
接下来是:我认为合理的短期解决方案是做其他驱动程序正在做的事情,并要求Marionette绑定到geckodriver生成的随机自由端口.目前,它使用2828作为Firefox生成的所有实例的默认值.由于Marionette遗憾的是还没有一种带外方式将端口传送回客户端(geckodriver),这本身就很活泼,但我们可以通过bug 1240830中的一个提议来改善未来的情况.
这个改变是在
Selenium 3.0.0.b2
* Updated Marionette port argument to match other drivers.
Run Code Online (Sandbox Code Playgroud)
我猜随机只能工作这么久.提出一个问题.您拥有的selenium,firefox和geckodriver版本可能需要修复代码.你可以回到使用Selenium 2.53.0和firefox esr 38.8,直到修复它为止.你的来电.
更新:试试
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('path/to/binary')
driver = webdriver.Firefox(firefox_binary=binary)
Run Code Online (Sandbox Code Playgroud)