Search code examples
selenium-webdriverselenium-chromedriver

What is the solution of this python selenium error?


from selenium import webdriver

browser = webdriver.Chrome("./chromedriver.exe")

browser.get("http://youtube.com")

I have chrome version 118.0.5993.118, but I downloaded chromedriver version 118.0.5993.70. It was because there was no chromedriver version 118.0.5993.118. Then I have problem running code above, even though it is gramatically correct.

Is it because of the wrong chromedriver version? Then what will be the solution? Thank you.


Solution

  • Below line is incorrect.

    browser = webdriver.Chrome("./chromedriver.exe")
    

    Solution: From selenium v4.10.0 and onwards, you cannot pass executable_path(chrome driver path) as an argument. You need to use Service Class to pass the chromedriver.exe path.

    Option 1: Passing chromedriver.exe path using Service class.

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    
    service = Service(executable_path='<full path>/chromedriver.exe')
    browser = webdriver.Chrome(service=service)
    browser.get("http://youtube.com")
    

    Option 2: This is the easiest option. If your selenium version is 4.6.0 or above, let Selenium's built-in tool Selenium Manager do the driver management for you. No need to set the chromedriver.exe explicitly. Code can be as simple as:

    from selenium import webdriver
    
    browser = webdriver.Chrome()
    browser.get("http://youtube.com")
    

    Refer this - https://stackoverflow.com/a/76463081/7598774