Nguyen Tran
Nguyen Tran

Reputation: 139

Selenium Opens Browser But Refuses to Execute the Next Line of Code

It looks like after opening the browser, Selenium cannot move on for some reasons I can't figure out. No error was ever displayed.

Here is my simple code:

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# Using Chrome to access web
browser = webdriver.Chrome(executable_path = "C:\Program Files\Google\Chrome\Application\chrome.exe")

print ("done")

# Open YouTube website
browser.get("https://www.youtube.com/")

The browser opens just fine, but the print("done") statement is never executed. (In the terminal the word "done" was never printed. So, it infers that the selenium has never finished executing the command to open the browser even though the browser has opened, and I have waited for several minutes.

Thanks in advance to our wonderful StackOverflow community!

Upvotes: 1

Views: 2458

Answers (4)

questioning
questioning

Reputation: 287

As mentioned above, this issue is the chrome.exe. You need to use a chromedriver instead. You can download one manually here https://chromedriver.chromium.org/downloads and then set the path to it like you did to the chrome.exe.

However, instead of downloading the chromedriver manually, I recommend using a library on GitHub which does that for you and loads it from cache if there is already one installed. (I'm not the owner nor the maintainer of this repository, but I do find it rather simple to use.)

https://github.com/SergeyPirogov/webdriver_manager

First you'd need to pip install webdriver-manager and then you can use it as following:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

Upvotes: 1

cruisepandey
cruisepandey

Reputation: 29362

The reason is because you are using chrome.exe which is for browser. Instead you should download chromdriver.exe, Please download from here. You should download Latest stable release: ChromeDriver 94.0.4606.61 (as on 3rd-oct-2021). Keep that in your automation directory and any directory of your preference.

driver_path = r'C:\\Users\\username\\Desktop\\Automation\\chromedriver.exe'
driver = webdriver.Chrome(executable_path = driver_path)

Note that, in place of driver_path, you should give the path where you've kept the chromdriver.exe

Upvotes: 1

pneumatic
pneumatic

Reputation: 31

Try these steps:

  1. Check your Google Chrome version here "chrome://settings/help"
  2. Download chromedriver.exe from "https://chromedriver.chromium.org/downloads"
  3. Change executable path to newly downloaded file.

Upvotes: 1

You should use chromedriver.exe instead of the path to your chrome.exe.
Download the chromedriver suitable for your chrome version, from here:
donwload Chromedriver.exe

Afterwards, do something like this:

browser = webdriver.Chrome("E:\YourPathToChromeDriver\chromedriver.exe")

Upvotes: 1

Related Questions