我构建了一个Flask应用,该应用允许用户输入URL。该应用程序调用一个利用Selenium的Firefox Webdriver出门并拍摄输入URL的屏幕截图的功能。我得到的错误是:
WebDriverException: Message: Process unexpectedly closed with status 127
我不确定我缺少什么帮助,将不胜感激。
这是堆栈跟踪:
File "/opt/scraper_ui/scraper_engine/simple_scraper.py", line 101, in
page_shot_stitched
driver = webdriver.Firefox(capabilities=cap,
firefox_options=options, executable_path='/usr/local/bin/geckodriver')
File "/opt/scraper_ui/scraper_ui_env/local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
keep_alive=True)
File "/opt/scraper_ui/scraper_ui_env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 156, in __init__
self.start_session(capabilities, browser_profile)
File "/opt/scraper_ui/scraper_ui_env/local/lib/python2.7/site-
packages/selenium/webdriver/remote/webdriver.py", line 251, in
start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/opt/scraper_ui/scraper_ui_env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "/opt/scraper_ui/scraper_ui_env/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: Process unexpectedly closed with status 127
这是功能代码:
def page_shot_stitched(**kwargs):
url = kwargs.pop('url', None)
site_folder = kwargs.pop('site_folder', None)
options = Options()
options.binary = '/usr/bin/firefox'
options.add_argument("--headless")
options.add_argument("--start-maximized")
try:
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = True
driver = webdriver.Firefox(capabilities=cap,
firefox_options=options, executable_path='/usr/local/bin/geckodriver')
driver.get(url)
js = 'return Math.max( document.body.scrollHeight,
document.body.offsetHeight, document.documentElement.clientHeight,
document.documentElement.scrollHeight,
document.documentElement.offsetHeight);'
scrollheight = driver.execute_script(js)
slices = []
offset = 0
while offset < scrollheight:
driver.execute_script("window.scrollTo(0, %s);" % offset)
img = Image.open(StringIO(driver.get_screenshot_as_png()))
offset += img.size[1]
slices.append(img)
screenshot = Image.new('RGB', (slices[0].size[0], scrollheight))
offset = 0
for img in slices:
screenshot.paste(img, (0, offset))
offset += img.size[1]
screenshot.save('{0}.png'.format(site_folder),'PNG')
driver.quit()
except Exception as e:
import traceback
print traceback.format_exc()
retriever_logger.error('Error {0}'.format(traceback.format_exc()))
不使用Flask时,调用该函数可以正常工作并拍摄屏幕截图。