What is selenium webdriver (Install selenium and dependencies)
The major tool used in this browser automation is selenium webdriver. What is selenium webdriver? Well, it’s a module that enables browser automation as you’ll see very soon. It basically allows you to mimic a person using a browser.
Let’s quickly move on to the business of the day. At this point you should have Python installed, whichever version you have is fine. Also, make sure your python folder is in the PATH environment variable as explained in the intro.
Install selenium with this command:
pip install selenium
If you don’t have pip, you can install it with the command
After installing selenium, the next thing you need to do is to download the browser drivers.
- Firefox: Download geckodriver (driver required to use Firefox with selenium) from this link. Don’t forget to check and choose the one that matches your system.
- Chrome: Download the latest chromedriver from this link or older releases from this link. As you may have guessed, this is for Chrome.
Once you download the zip file, unzip it and copy it into the python folder (or any directory in path to allow selenium access it during runtime).
This won’t be needed now but you should install it as we’ll make use of it later. Pandas is a very powerful python module for manipulating and handling spreadsheets. We may not use it extensively since we’re more interested in automating a browser and not manipulating data.
Install with this command:
pip install pandas
Let’s do our first ever browser automation and see how it goes.
The code below opens Firefox. Make sure you have Firefox, else change that line to
driver = webdriver.Chrome() if you use chrome.
from selenium import webdriver URL = 'http://google.com' driver = webdriver.Firefox()
I believe you’ve seen the line
URL = 'http://google.com'. Yes, your guess is right, we are going to open google and do a search. Update your code to the below and run it to open Google.
from selenium import webdriver URL = 'http://google.com' driver = webdriver.Firefox() driver.get(URL)
We open a url using the
get method. Try it out.
Now let’s enter something into the powerful search engine and hit ENTER. Wait! I didn’t mean do it manually, we’ll write code to do that automatically. 😀
from selenium import webdriver from selenium.webdriver.common.keys import Keys URL = 'http://google.com' driver = webdriver.Firefox() driver.get(URL) text_input = driver.find_element_by_name('q') text_input.send_keys('hello world') text_input.send_keys(Keys.ENTER)
We add a second import statement that allows us to use special Keys on the keyboard. Also, congratulations on using your first html tag finding method,
find_element_by_name. Open google manually, right click, inspect the webpage and check the input tag that takes the search term. Remember the short video tutorial on that? Here:
The input has a name attribute ‘q’ and that’s what the method does. It captures tags based on names. There are a lot of other methods like that for id, class etc.
We enter the text ‘hello world’ and press ENTER by sending
Hope you’ve enjoyed it so far. More to come stay focused! 😀 😀