在DigitalOcean中运行无头Firefox或Chrome

时间:2018-11-20 18:56:37

标签: selenium selenium-webdriver web-scraping selenium-firefoxdriver

我正在尝试在DigitalOcean中运行无头的Chrome或Firefox,并尝试了很多解决方案,但似乎都没有用。

该代码在我的本地系统中工作出色,但是在我的DigitalOcean服务器中却没有。

这是我用来测试无头Firefox的测试

from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.firefox.options import Options


# print("open virtual display")
with Display():
    print("open Firefox browser")
    browser = webdriver.Firefox()
    browser.set_window_size(1120, 550)

    url = 'http://arbspiper.com/'
    browser.get(url)

    title = browser.title
    print(title)
    browser.quit()

我得到的错误是

Traceback (most recent call last):
    File "firefox.py", line 9, in <module>
      browser = webdriver.Firefox()
    File "/home/arbspiper_project/env/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
keep_alive=True)
    File "/home/arbspiper_project/env/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
    File "/home/arbspiper_project/env/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
    File "/home/arbspiper_project/env/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
    File "/home/arbspiper_project/env/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.WebDriverException: Message: newSession

Mozilla Firefox 64.0 硒版本(3.141.0)

1 个答案:

答案 0 :(得分:0)

来自 Manoj Kengudelu 的评论的答案。

gekodriver必须与您使用的Firefox版本兼容。 查看supported platforms table here

一旦找到了想要使用的gekodriver,就可以下载它,使其可执行并添加到路径中。

再次感谢:Manoj Kengudelu