- Published on
Web automation by Selenium (Python)
- Authors
- Name
- Tun Tauk
Web automation automates websites by using some scripting languages. Selenium is an open-sourced web automation framework and it supports C#, Python, Java, Javascript, and Kotlin. The main purpose is for validating and testing for websites but it is also used for web scrapping. Moreover, other boring stuff can be done by using selenium and it is quite fun to learn and use Selenium.
Prerequisite:
install Python. (make sure python and pip can be used from the terminal.)
install Selenium from pip:
pip install seleniumdownload the web driver related to your browser:
For me, I used chrome, so I download the chrome driver. (make sure your driver version and browser version are the same)
Let's start some code.
- First, to import the webdriver.
from selenium import webdriver
driver = webdriver.Chrome("path/to/yourdriver")
get()function to surf the website
driver.get("http://www.google.com")
use the
find_element()function to find an elementThere have several functions.
- find_element_by_id()
- find_element_by_name()
- find_element_by_class_name()
There are also other functions to find an element. I will show you later.
To get the input text field of google search, we can use find_element_by_name("q"). This can be known by using inspect element function of the browser.
search = driver.find_element_by_name("q")
- To input values, we use send_keys() function
search.send_keys("hello world")
- To press
ENTERkey, we need toimport Keys
from selenium.webdriver.common.keys import Keys
search.send_keys("hello world",Keys.ENTER)
- Final code will be:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("path/to/yourdriver")
driver.get("http://www.google.com")
search = driver.find_element_by_name("q")
search.send_keys("hello world",Keys.ENTER)
- save the file and run from the terminal.
python filename.py
This is some basic of Selenium. I will write about some more functions of Selenium, later.