selenium.common.exceptions.WebDriverException: Mes

2019-08-24 20:05发布

问题:

I just tried to get an easy Python script to work, which should only open up google.

I installed selenium with pip and placed the operadriver, downloaded from the selenium page, into my python path. Also watched many videos about it, but I can't find a solution..

Here's the code:

from selenium import webdriver
import time

driver = webdriver.Opera()
driver.get('http://www.google.com')

The error:

Traceback (most recent call last):
  File "C:/Users/Tom/AppData/Local/Programs/Python/Python37-32/Scripts/automate.py", line 4, in <module>
    driver = webdriver.Opera()
  File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\opera\webdriver.py", line 83, in __init__
    service_log_path=service_log_path)
  File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\opera\webdriver.py", line 62, in __init__
    keep_alive=keep_alive)
  File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Opera binary
  (Driver info: OperaDriver=2.40 (a50783a565882ef2022bea655e8560f37ecf8afe),platform=Windows NT 6.1.7601 SP1 x86_64)

回答1:

This error message...

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Opera binary
  (Driver info: OperaDriver=2.40 (a50783a565882ef2022bea655e8560f37ecf8afe),platform=Windows NT 6.1.7601 SP1 x86_64)

...implies that the Opera Browser binary wasn't found at the required location.

Your main issue is the Opera Browser is not installed at the default location. So you need to mention the absolute path of the location where Opera Browser is installed as follows:

from selenium import webdriver
from selenium.webdriver.opera.options import Options

options = Options()
options.binary_location = r'C:\path\to\opera.exe'
driver = webdriver.Opera(opera_options = options, executable_path=r'C:\Utility\BrowserDrivers\operadriver.exe')
driver.get('http://www.google.com')