Возможен ли безголовый селен Firefox в Mac OS X 10.12.16? - PullRequest
0 голосов
/ 29 июня 2018

Я могу запустить веб-драйвер Firefox, используя selenium в Python, но когда я пытаюсь использовать тот же код при добавлении параметра для -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

1 Ответ

0 голосов
/ 29 июня 2018

Как на сайте 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()
...