Drive Opera with selenium python

风格不统一 提交于 2019-11-28 11:47:24

Based on your question it looks like you are using an old driver for Opera version 12 and older. Assuming that you're trying to use the most recent version of Opera you'll want to use the driver available at the following site: OperaChromiumDriver

The site lists sample python code which I have confirmed works on my machine to open and drive Opera: Python Examples for OperaChromiumDriver

import time

from selenium import webdriver
from selenium.webdriver.chrome import service


webdriver_service = service.Service('C:\\Users\\Kris\\Downloads\\WinPython-32bit-2.7.9.2\\operadriver.exe')
webdriver_service.start()

driver = webdriver.Remote(webdriver_service.service_url, webdriver.DesiredCapabilities.OPERA)

driver.get('https://www.google.com/')
input_txt = driver.find_element_by_name('q')
input_txt.send_keys('operadriver\n')

time.sleep(5) #see the result
driver.quit()

EDIT: Looking into the change logs for selenium it looks like support for Presto-based Operas was discontinued after 2.45 which is why you're getting the error messages:CHANGELOG

The code below worked for me on the following setup. Hope this helps.

  • Python: 3.4.0
  • Opera: 42.0.2393.94
  • Selenium: 3.0.2
  • ChromeDriver: 2.27.440174

    import os
    
    from selenium import webdriver
    from selenium.webdriver.common import desired_capabilities
    from selenium.webdriver.opera import options
    
    _operaDriverLoc = os.path.abspath('E:\\Envs\\PySelEnv\\selserver\\chromedriver.exe')  # Replace this path with the actual path on your machine.
    _operaExeLoc = os.path.abspath('E:\\Program Files\\Opera\\42.0.2393.94\\opera.exe')   # Replace this path with the actual path on your machine.
    
    _remoteExecutor = 'http://127.0.0.1:9515'
    _operaCaps = desired_capabilities.DesiredCapabilities.OPERA.copy()
    
    _operaOpts = options.ChromeOptions()
    _operaOpts._binary_location = _operaExeLoc
    
    # Use the below argument if you want the Opera browser to be in the maximized state when launching. 
    # The full list of supported arguments can be found on http://peter.sh/experiments/chromium-command-line-switches/
    _operaOpts.add_argument('--start-maximized')   
    
    self._browserDriver = webdriver.Chrome(executable_path = _operaDriverLoc, chrome_options = _operaOpts, desired_capabilities = _operaCaps)
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!