nie można wywołać Firefoksa z selenium w Pythonie na maszynie AWS

Próbuję użyć selenium z Pythona do zeskrobania niektórych stron dynamiki za pomocą javascript. Nie mogę jednak wywołać Firefoksa po wykonaniu instrukcji selenium na stronie pypi(http://pypi.python.org/pypi/selenium). zainstalowałem Firefoksa na AWS ubuntu 12.04. Otrzymany komunikat o błędzie to:

In [1]: from selenium import webdriver

In [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'

Szukałem w sieci i okazało się, że ten problem wystąpił u innych ludzi (https://groups.google.com/forum/?fromgroups=#! temat / selenium-users/21sjrojulzy). ale nie rozumiem rozwiązanie, jeśli jest.

Czy ktoś może mi pomóc? Dzięki!
Author: David, 2012-10-24

3 answers

Problem polega na tym, że Firefox wymaga wyświetlacza. Użyłem pyvirtualdisplay w moim przykładzie do symulacji wyświetlacza. Rozwiązaniem jest:

from pyvirtualdisplay import Display
from selenium import webdriver

display = 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()

Należy pamiętać, że pyvirtualdisplay wymaga jednego z następujących back-endów: Xvfb, Xephyr, Xvnc.

To powinno rozwiązać twój problem.
 52
Author: That1Guy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-07 21:14:15

Ja też miałem ten sam problem.Byłem na Firefox 47 i Selenium 2.53. Więc to, co zrobiłem, to zmniejszyłem Firefoksa do 45. To zadziałało.

1) Usuń Firefoksa 47 najpierw:

sudo apt-get purge firefox

2) Sprawdź Dostępne wersje:

apt-cache show firefox | grep Version

Wyświetli Dostępne wersje Firefoksa takie jak:

Version: 47.0+build3-0ubuntu0.16.04.1

Version: 45.0.2+build1-0ubuntu1

3) powiedz, który build pobrać

sudo apt-get install firefox=45.0.2+build1-0ubuntu1

4) Następnie nie musisz uaktualniać do nowszej wersji jeszcze raz.

sudo apt-mark hold firefox

5) Jeśli chcesz uaktualnić później

sudo apt-mark unhold firefox sudo apt-get upgrade

Mam nadzieję, że to pomoże.
 4
Author: Amogh Joshi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-11 03:44:49

To już jest w komentarzu do pytania OP, ale żeby to wyjaśnić jako odpowiedź. Selenium można uruchamiać w tle bez otwierania rzeczywistego okna przeglądarki.

Na przykład, jeśli używasz Chrome, ustaw następujące opcje:

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.set_headless(headless=False)

Następnie po wywołaniu sterownika web, ustawienia stają się parametrem:

browser = webdriver.Chrome(chrome_options=chrome_options)
 0
Author: David Skarbrevik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-31 18:54:47