在Mac OS X 10.12.16上可以使用无头硒Firefox吗?

时间:2018-06-29 18:27:31

标签: python python-3.x selenium selenium-webdriver headless

我能够在Python中使用Selenium来运行Firefox Webdriver,但是当我在添加“ -headless”选项的同时尝试相同的代码时,会回溯到"SessionNotCreatedException: Message: Unable to find a matching set of capabilities"

是否有可能使其正常运行,有人可以分享他们的工作方式吗?

这是我正在运行的:

  • Python v。3.5.0
  • Firefox v。57.0.4
  • 硒v.3.11.0
  • geckodriver v。0.21.0 [推荐与Firefox 57+和硒一起使用 3.11.0 +]

我的文件:

import os
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.chrome.options import Options

# executable path == '/Users/helloorange/geckodriver'
executable_path = os.path.expanduser("~/geckodriver")
# firefox_profile == '/Users/helloorange/Library/Application Support/Firefox/Profiles/k9a5czjm.Default User'
firefox_profile = "%s/%s" % (os.path.expanduser("~/Library/Application Support/Firefox/Profiles"), os.listdir(os.path.expanduser("~/Library/Application Support/Firefox/Profiles"))[0])
firefox_binary = FirefoxBinary("/Applications/Firefox.app/Contents/MacOS/firefox") # Folder for Firefox v 57
os.system("chmod a+x '%s'"%executable_path)

options = Options()
options.add_argument("--headless")
options.binary = firefox_binary
options.profile = firefox_profile

wd = webdriver.Firefox(executable_path=executable_path, firefox_profile=firefox_profile, firefox_binary=firefox_binary, firefox_options=options, log_path="geckodriver.log")
wd.get('http://google.com/')
print(wd.current_url)
wd.quit()

我的回溯:

---------------------------------------------------------------------------
SessionNotCreatedException                Traceback (most recent call last)
<ipython-input-2-8fdf4535f36e> in <module>()
     23 options.profile = firefox_profile
     24 
---> 25 wd = webdriver.Firefox(executable_path=executable_path, firefox_profile=firefox_profile, firefox_binary=firefox_binary, firefox_options=options, log_path="geckodriver.log")#,capabilities=capabilities)
     26 wd.get('http://google.com/')
     27 print(wd.current_url)

~/venv/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py in __init__(self, firefox_profile, firefox_binary, timeout, capabilities, proxy, executable_path, options, log_path, firefox_options, service_args)
    160                 command_executor=executor,
    161                 desired_capabilities=capabilities,
--> 162                 keep_alive=True)
    163 
    164         # Selenium remote

~/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py in __init__(self, command_executor, desired_capabilities, browser_profile, proxy, keep_alive, file_detector, options)
    152             warnings.warn("Please use FirefoxOptions to set browser profile",
    153                           DeprecationWarning)
--> 154         self.start_session(desired_capabilities, browser_profile)
    155         self._switch_to = SwitchTo(self)
    156         self._mobile = Mobile(self)

~/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py in start_session(self, capabilities, browser_profile)
    241         parameters = {"capabilities": w3c_caps,
    242                       "desiredCapabilities": capabilities}
--> 243         response = self.execute(Command.NEW_SESSION, parameters)
    244         if 'sessionId' not in response:
    245             response = response['value']

~/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    310         response = self.command_executor.execute(driver_command, params)
    311         if response:
--> 312             self.error_handler.check_response(response)
    313             response['value'] = self._unwrap_value(
    314                 response.get('value', None))

~/venv/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

SessionNotCreatedException: Message: Unable to find a matching set of capabilities

2 个答案:

答案 0 :(得分:0)

根据mozilla网站,这是打开无头模式的方法:

from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as expected
from selenium.webdriver.support.wait import WebDriverWait

if __name__ == "__main__":
    options = Options()
    options.add_argument('-headless')
    driver = Firefox(executable_path='geckodriver', firefox_options=options)
    wait = WebDriverWait(driver, timeout=10)
    driver.get('http://www.google.com')
    wait.until(expected.visibility_of_element_located((By.NAME, 'q'))).send_keys('headless firefox' + Keys.ENTER)
    wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, '#ires a'))).click()
    print(driver.page_source)
    driver.quit()

答案 1 :(得分:0)

基于@PJAutomator的答案,此结果在我的计算机上显示:

Traceback (most recent call last):   File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1741, in <module>
    main()   File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1735, in main
    globals = debugger.run(setup['file'], None, None, is_module)   File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1135, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script   File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)   File "/Users/nathan/Library/Preferences/PyCharmCE2018.3/scratches/scratch_2.py", line 23, in <module>
    wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, '#ires a'))).click()   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: