Search code examples
python-webbrowser

Issue opening a webpage with Chrome using 'webbrowser'


My default web browser is Firefox. When I run this code:

import webbrowser
webbrowser.open("https://google.com")

Firefox shows up and opens the specified url page.

But I want to open the page with Chrome. So I tried:

chrome_path = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
webbrowser.get(chrome_path).open("https://google.com")

Then I get Error: could not locate runnable browser message.

What am I doing wrong?


Solution

  • You need to use the chrome driver to run it. You can download ChromeDriver here: https://sites.google.com/a/chromium.org/chromedriver/downloads

    Then you have multiple options:

    • add it to your system path

    • put it in the same directory as your python script

    • specify the location directly via executable_path

       driver = webdriver.Chrome(executable_path='C:/path/to/chromedriver.exe')
      

    Upd: To handle it you can go it:

    in cmd line

    pip install webdriver-manager
    

    and in python

    from selenium import webdriver
    
    from webdriver_manager.chrome import ChromeDriverManager
    
    driver = webdriver.Chrome(ChromeDriverManager().install())