Step one, you need to get chromedriver.
What is chromedriver? It is a separate binary that you must run to get Selenium and Chrome working. See for tiny explantion of what chromedriver does : https://dev.to/tonetheman/chromedriver-role-in-the-world-c06
To download chromedriver (which you must have to use Chrome with Selenium) go to this link: http://chromedriver.chromium.org/downloads
Mainly note the version of Chrome you are using. I am using Windows 10 and this Chrome: Version 75.0.3770.142 (Official Build) (64-bit) So I will pick this version of chromedriver: https://chromedriver.storage.googleapis.com/index.html?path=75.0.3770.140/
You need to save the chromedriver.exe to a directory that you will remember or your working directory. You need to know where you saved chromedriver.exe because you will use the location in the Python script that you are about to write.
from selenium import webdriver ## note 1 driver = None try: cpath = "e:\\projects\\headless\\chromedriver.exe" ## note 2 driver = webdriver.Chrome(cpath) ## note 3 driver.get("https://google.com") ## note 4 import time ## note 5 time.sleep(3) finally: # note 6 if driver is not None: driver.close()
Note 1 - this is where you are loading the Python webdriver binding. This is a fancy way of saying we are telling Python about Selenium. If you do not have this line none of your Selenium scripts will run.
Note 2 - this is the path of where I put chromedriver.exe your directory name will be different. The name does not matter either just pick somewhere on your disk.
Note 3 - this is where Chrome will start up. Chrome and chromedriver.exe are both started on this line. If you looked at your processlist at the instant that line executes you will see a new Chrome instance start along with a chromedriver.exe. If you look closely chromedriver.exe starts first and it starts Chrome.exe
Note 4 - this line navigates to google. Not exciting but at this point you will see your Selenium driven Chrome navigate to a web page. woooo!!!!
Note 5 - at this point I put in a sleep so you can actually see what is happening. In general sleeps are bad when you are writing scripts. There are times when you are debugging when time.sleep is useful. This is one of those cases.
Note 6 - this is shutting down chromedriver.exe and Chrome. You need this for cleanup. If you did not run that line Chrome.exe will still continue to run until you stop it manually.
And that is it. Your first Selenium script with Python.