我正在尝试使用Selenium和Xvfb在Amazon EC2环境中使用Python以无头模式连接到Firefox。我已经尝试了大部分工作组合但没有结果。
from xvfbwrapper import Xvfb
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium import webdriver
import os
try:
vdisplay = Xvfb()
vdisplay.start()
file_entity = open("/home/ubuntu/downloads/fflog.txt", 'wb')
temp_folder = '/home/ubuntu/downloads/ffData'
dir =''
if not os.path.exists(temp_folder):
dir = os.makedirs(temp_folder)
ffProfile = webdriver.FirefoxProfile(profile_directory=temp_folder)
binary = FirefoxBinary(log_file= file_entity)
driver = webdriver.Firefox(firefox_binary=binary)
finally:
vdisplay.stop()
我得到的例外:
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 64, in __init__
self.binary, timeout),
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 51, in __init__
self.binary.launch_browser(self.profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 70, in launch_browser
self._wait_until_connectable()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 105, in _wait_until_connectable
raise WebDriverException("**Can't load the profile. Profile " selenium.common.exceptions.WebDriverException:** **Message: Can't load the profile. Profile Dir: %s If you specified a log_file in the FirefoxBinary constructor, check it for details.**
这是来自FirefoxBinary(fflog.txt)的日志数据:
1434536187380 addons.manager DEBUG Provider finished startup:<unnamedprovider>
1434536187459 DeferredSave.extensions.json
DEBUG Starting timer 1434536187492 DeferredSave.extensions.json
DEBUG Starting write 1434536187566 DeferredSave.extensions.json
DEBUG Write succeeded 1434536187566 addons.xpi-utils
DEBUG XPI Database saved, setting schema version preference to 16
1434536188072 addons.repository DEBUG No addons.json
found. 1434536188072 DeferredSave.addons.json DEBUG Save
changes 1434536188074 DeferredSave.addons.json DEBUG
Starting timer 1434536188170 DeferredSave.addons.json DEBUG
Starting write 1434536188176 DeferredSave.addons.json DEBUG
Write succeeded