我有一个使用Python3.6运行时的aws lambda函数,带有firefox69,geckodriver25和selenium3.14的3个lambda层,我不确定从哪个方向使firefox在lambda上运行。
这是我的代码:
#from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
path = '/tmp/'
# FIREFOX
def start_browser(url, headless=True):
try:
fp = webdriver.FirefoxProfile()
options = Options()
if headless==True:
options.headless = True
#caps = DesiredCapabilities.FIREFOX.copy()
#caps['marionette'] = False
fp.set_preference("browser.preferences.instantApply",True)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/xls, application/vnd.ms-excel")
fp.set_preference("browser.helperApps.alwaysAsk.force",False)
fp.set_preference("browser.download.dir", path)
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
browser = webdriver.Firefox(
options=options,
#capabilities=caps,
executable_path="/opt/python/geckodriver",
log_path="/tmp/geckodriver.log",
firefox_profile=fp,
firefox_binary="/opt/python/firefox/firefox"
)
browser.get(url)
return browser
except Exception as e:
print(e)
finally:
f = open('/tmp/geckodriver.log', 'r')
file_contents = f.read()
print("LOG FILE CONTENT: " + file_contents + "END")
f.close()
在lambda中运行以上命令会给我错误
Message: invalid argument: can't kill an exited process
但是,如果我取消注释“大写”行,则会出现此错误
Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.
但是我的geckodriver.log是空的