Codementor Events

Quick Dive into Selenium with python

Published Jan 14, 2020Last updated Jan 17, 2020
Quick Dive into Selenium with python

Image credit: xnathan.com

Hi guys, I am chris, a software engineer and I have been building stuff with python since 2016.
This would be a fast paced introduction to selenium.

What is Selenium?

In simple terms, selenium is a tool used to automate browsers, in even simpler terms selenium can be used to control broswers. To find out more visit the selenium site

Usage

  • To control a browser, you need to install the browser's driver, learn to install the drivers here For example: To control firefox, install firefox's driver.

  • You need to install a python library for selenium. Use pip install selenium to install. Docs can be found here

Tip: Install in a virtual environment, personally I use pipenv to manage my virtual environments because I think the commands are much easier to use.

  • And finally, wait for it, a place to write your code!

An example illustrating some common operations

  • Opening a page, in this example the google home page.

    • Import webdriver from the selenium library from selenium import webdriver
    • Start a browser browser = webdriver.Firefox() to start firefox or browser = webdriver.Chrome() to start chrome. Ensure you have the browser's drive installed.

    Tip: after starting the browser, it is advisable to run browser.implicitly_wait(30). This waits for 30 seconds for a command or element to be found. This is very useful in various situations eg: when your internet connection is poor making the page load slowly.

    • Navigate to the page browser.get("https://www.google.com/")
  • Typing into an html element, in this case the google search box

    • First off, find the element using search_box = browser.find_element_by_name("q") q is the name of the search box, found by inspecting the page.
    • Writing in the element search_box.send_keys("google"). This writes google to the search box.

    Tip: Use search_box.clear() to clear the previous text in the element.

  • Clicking on an element, in this case the search button

    • First off, find the element search_button = browser.find_element_by_name("btnK")
    • Click on button search_button.click()
      Result: This should show a page containing the google search results for google, haha.
  • Closing the browser, use browser.quit() to close the browser.

So Chris, where can I use selenium?

  • I have used selenium for browser based tests(eg: scenario based testing etc.). You can also add it to your CI/CD pipeline as selenium plays well with headless browsers(browsers without a gui).

  • Automation of adminstration tasks. For example I have used selenium to automate some network adminstration tasks, i built a cli app, leveraging selenium, using a python library called click to achieve this.

  • Anywhere else, where you need to control a browser.

Comment, like and share if you need/want to. Thanks for reading!

Discover and read more posts from Chris Ilozulu
get started
post comments2Replies
Narendra Kumar Vadapalli
5 years ago

Just figured, you could directly use search_box.submit() to submit the search keywords after filling them in, rather than finding he search button !

Chris Ilozulu
5 years ago

Yeah, that is cool. I believe that is because it is part of a form. I wonder if search_box.submit() would work if it is not the only element, asides the submit button, in the form.