Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to automate your webdriver

How to automate your webdriver

In this tutorial you gonna learn how to automate your browser with Selenium.

Selenium is a Python package for communicating with a webdriver.

Importing Selenium

First you need to import Selenium:

Now we need a topic to look up on Google:

Connect with your webdriver

Then let's connect to the driver with webdriver.Firefox():

When we have a connection with the driver, we need to look up an URL with the help of driver.get(URL):

Now that we've looked up the URL, let's get the elements that Google returned.

How to find elements

There are a few ways to find elements:

  • find_element_by_id()
  • find_element_by_name()
  • find_element_by_xpath()
  • find_element_by_link_text()
  • find_element_by_partial_link_text()
  • find_element_by_tag_name()
  • find_element_by_class_name()
  • find_element_by_css_selector()

These ways only fetch one element. But we want all the elements, you can use these commands for that:

  • find_elements_by_name()
  • find_elements_by_xpath()
  • find_elements_by_link_text()
  • find_elements_by_partial_link_text()
  • find_elements_by_tag_name()
  • find_elements_by_class_name()
  • find_elements_by_css_selector()

To retrieve the elements, use this piece of code:

The code is going to find a-elements (links) in divs with the class: yuRUbf. Google uses the classname yuRUbf for the div where the link in is.

Printing the URLs

Now that we have the links, we're just going to print them out:

To print the link we use .get_attribute("href")

Then all we have to do is close the driver with driver.close()

Now the program is ready, so test it.

Final Code

Bonus: YouTube bot

You can also make this program work on YouTube. You just need to change the URL and the classes.

Here is the YouTube code:

I hope you enjoyed the tutorial with making your own Google and YouTube.