My selenium code throwing error while using opera browser for the first time

1k Views Asked by At

I am using opera version=91.0.4516.16 and operadriver version = 105.0.5195.102. I'm unable to opera browser with the simplest code also which is:

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

s = Options()
s.binary_location = r'C:\Users\Admin\AppData\Local\Programs\Opera\launcher.exe'
driver = webdriver.Opera( executable_path=r'F:\ffmpeg\bin\operadriver.exe')
driver.get("http://google.com/")

I have used varied combination of codes but the same error occurs again and again.

ERROR IS:

Traceback (most recent call last):
  File "r:\Youtube\operatest.py", line 96, in <module>
    driver = webdriver.Opera( executable_path=r'F:\ffmpeg\bin\operadriver.exe')
  File "R:\avirtual\lib\site-packages\selenium\webdriver\opera\webdriver.py", line 75, in __init__
    OperaDriver.__init__(self, executable_path=executable_path,
  File "R:\avirtual\lib\site-packages\selenium\webdriver\opera\webdriver.py", line 51, in __init__
    ChromiumDriver.__init__(self,
  File "R:\google_account_Maker\avirtual\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 70, in __init__        
    super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "R:\avirtual\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 92, in __init__      
    RemoteWebDriver.__init__(
  File "R:\avirtual\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 275, in __init__       
    self.start_session(capabilities, browser_profile)
  File "R:\avirtual\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 365, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "R:\avirtual\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 430, in execute        
    self.error_handler.check_response(response)
  File "R:\avirtual\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Missing or invalid capabilities
  (Driver info: operadriver=105.0.5195.102 (4c16f5ffcc2da70ee2600d5db77bed423ac03a5a-refs/branch-heads/5195_55@{#4}),platform=Windows NT 10.0.19042 x86_64)

Edit 2:

After changing my code. WHich is:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.opera import OperaDriverManager
# from selenium.webdriver.opera.options import Options

options = webdriver.ChromeOptions()
options.add_argument('allow-elevated-browser')
options.add_experimental_option('w3c', True)
options.binary_location = r"C:\Users\Admin\AppData\Local\Programs\Opera\launcher.exe"
driver = webdriver.Chrome(executable_path=OperaDriverManager().install(), options=options)
driver.get("https://www.google.com")

The Error is:

r:\google_account_Maker\Youtube_channels_maker\operatest.py:120: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(executable_path=OperaDriverManager().install(), options=options)
Traceback (most recent call last):
  File "r:\google_account_Maker\Youtube_channels_maker\operatest.py", line 120, in <module>
    driver = webdriver.Chrome(executable_path=OperaDriverManager().install(), options=options)
  File "R:\google_account_Maker\avirtual\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 70, in __init__        
    super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "R:\google_account_Maker\avirtual\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 92, in __init__      
    RemoteWebDriver.__init__(
  File "R:\google_account_Maker\avirtual\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 275, in __init__       
    self.start_session(capabilities, browser_profile)
  File "R:\google_account_Maker\avirtual\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 365, in start_session  
    response = self.execute(Command.NEW_SESSION, parameters)
  File "R:\google_account_Maker\avirtual\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 430, in execute        
    self.error_handler.check_response(response)
  File "R:\google_account_Maker\avirtual\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Opera failed to start: exited normally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from opera location C:\Users\Admin\AppData\Local\Programs\Opera\launcher.exe is no longer running, so OperaDriver is assuming that Opera has crashed.)
Stacktrace:
Backtrace:
        Ordinal0 [0x00007FF69BAE84A2+2720930]
        Ordinal0 [0x00007FF69BA5EF02+2158338]
        Ordinal0 [0x00007FF69B939637+955959]
        Ordinal0 [0x00007FF69B961D71+1121649]
        Ordinal0 [0x00007FF69B95CF6A+1101674]
        Ordinal0 [0x00007FF69B99A829+1353769]
        Ordinal0 [0x00007FF69B994B63+1330019]
        Ordinal0 [0x00007FF69B967E6C+1146476]
        Ordinal0 [0x00007FF69B969298+1151640]
        GetHandleVerifier [0x00007FF69BDFD5F8+3154216]
        GetHandleVerifier [0x00007FF69BDEE40D+3092285]
        GetHandleVerifier [0x00007FF69BB9488C+627644]
        GetHandleVerifier [0x00007FF69BB93643+622963]
        Ordinal0 [0x00007FF69BA67DDF+2194911]
        Ordinal0 [0x00007FF69BA6D284+2216580]
        Ordinal0 [0x00007FF69BA6D3FB+2216955]
        Ordinal0 [0x00007FF69BA79F30+2268976]
        BaseThreadInitThunk [0x00007FF804EC7034+20]
        RtlUserThreadStart [0x00007FF8069C2651+33]

I can't understand why the opera browseris crashing again and again. Please help me to understand.

2

There are 2 best solutions below

0
On

Solution

With selenium==4.2.0 You can solve your issue.

Also one more thing, the reason why we can't use opera in our latest version of selenium is that opera don't follow the rules of w3 that's why it was removed by selenium

from webdriver_manager.opera import OperaDriverManager
from selenium import webdriver

options = webdriver.ChromeOptions()
opera_profile = r"C:\\Users\\LEGEND\\AppData\\Roaming\\Opera Software\\Opera Stable"

options.add_argument('user-data-dir=' + opera_profile)
options._binary_location = r'C:\\Users\\LEGEND\\AppData\\Local\\Programs\\Opera\\launcher.exe'
options = webdriver.ChromeOptions()
options.add_argument('allow-elevated-browser')
options.add_experimental_option('w3c', True)
driver = webdriver.Opera(executable_path=OperaDriverManager().install(), options=options)
driver.get('https://whatsmyip.com')
driver.quit()

I Hope it resolves your issue to use operadriver.exe

2
On

Solution

With selenium4 as the key executable_path is deprecated you have to use an instance of the Service() class along with ChromeDriverManager().install() command as discussed below.

Pre-requisites Ensure that:

Selenium is upgraded to v4.0.0

pip3 install -U selenium

Webdriver Manager for Python is installed

pip3 install webdriver-manager

Selenium v4 compatible Code Block

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.google.com")

Added the answer for Chrome, make sure to change for opera.