繁体   English   中英

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

[英]Is headless selenium Firefox on Mac OS X 10.12.16 possible?

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

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

这是我正在运行的:

  • Python版本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

根据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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM