Selenium Tutorial: Web Scraping with Selenium and Python

Imagine what would you do if you could automate all the repetitive and boring activities you perform using internet, like checking every day the first results of Google for a given keyword, or download a bunch of files from different websites.

In this post you’ll learn to use Selenium with Python, a Web Scraping tool that simulates a user surfing the Internet. For example, you can use it to automatically look for Google queries and read the results, log in to your social accounts, simulate a user to test your web application, and anything you find in your daily live that it’s repetitive. The possibilities are infinite! 🙂

*All the code in this post has been tested with Python 2.7 and Python 3.4.

Install and use Selenium

Selenium is a python package that can be installed via pip. I recommend that you install it in a virtual environment (using virtualenv and virtualenvwrapper).

No virtualenv or virtualenvwrapper?

To install selenium, you just need to type:

In this post we are going to initialize a Firefox driver — you can install it by visiting their website. However, if you want to work with Chrome or IE, you can find more information here.

Once you have Selenium and Firefox installed, create a python file, selenium_script.py. We are going to initialize a browser using Selenium:

This just initializes a Firefox instance, waits for 5 seconds, and closes it.

Well, that was not very useful…

How about if we go to Google and search for something?

Web Scraping Google with Selenium

Let’s make a script that loads the main Google search page and makes a query to look for “Selenium”:

In the previous code:

  • the function init_driver  initializes a driver instance.
    • creates the driver instance
    • adds the  WebDriverWait function as an attribute to the driver, so it can be accessed more easily. This function is used to make the driver wait a certain amount of time (here 5 seconds) for an event to occur.
  • the function lookup  takes two arguments: a driver instance and a query lookup (a string).
    • it loads the Google search page
    • it waits for the query box element to be located and for the button to be clickable. Note that we are using the  WebDriverWait function to wait for these elements to appear.
    • Both elements are located by name. Other options would be to locate them by ID, XPATH, TAG_NAME, CLASS_NAME, CSS_SELECTOR , etc (see table below). You can find more information here.
    • Next, it sends the query into the box element and clicks the search button.
    • If either the box or button are not located during the time established in the wait function (here, 5 seconds), the TimeoutException  is raised.
  • the next statement is a conditional that is true only when the script is run directly. This prevents the next statements to run when this file is imported.
    • it initializes the driver and calls the lookup function to look for “Selenium”.
    • it waits for 5 seconds to see the results and quits the driver

Finally, run your code with:

Did it work? If you got an ElementNotVisibleException , keep reading!

How to catch an ElementNotVisibleExcpetion

Google search has recently changed so that initially, Google shows this page:

Google-Search

and when you start writing your query, the search button moves into the upper part of the screen.

Query-Google-Search

Well, actually it doesn’t move. The old button becomes invisible and the new one visible (and thus the exception when you click the old one: it’s not visible to click!).

We can update the lookup function in our code so that it catches this exception:

  • the element that raised the exception, button.click() is inside a try statement.
  • if the exception is raised, we look for the second button, using  visibility_of_element_located to make sure the element is visible, and then click this button.
  • if at any time, some element is not found within the 5 second period, the TimeoutException  is raised and caught by the two end lines of code.
  • Note that the initial button name is “btnK” and the new one is “btnG”.

Method list in Selenium

To sum up, I’ve created a table with the main methods used here.

Note: it’s not a python file — don’t try to run/import it 🙂

That’s all! Hope it was useful! 🙂

Don’t forget to share it with your friends!

Google+TwitterLinkedInFacebookReddit

Please, add +Marina Mele in your comments. This way I will get a notification email and I will answer you as soon as possible! :-)