- Selenium is a Python library and tool for automating web browsers to do a number of tasks like web scraping to extract useful data and information
- Selenium Remote Control - Replaced by Webdriver
- Selenium Web Driver
- Selenium Grid - Allows running of tests on multiple machines.
- Selenium IDE - Involves installing the extension in your browser
- Controls the behaviour of web browsers.
- Each browser has a driver(by default) that handles communication between Selenium and the browser.
- Web driver uses browser automation APIs to control the browser and run tests.
- It acts like a real user navigating through the browser.
- Used to develop Selenium test cases
- It records user's actions in the browser.
- After the development of the WebDriver tests, you may face the need to run your tests on multiple browsers and operating system combinations. This is where Grid comes into the picture
Installation
pip install selenium
- Incase it doesn't work on linux, use a virtual environment.
- All Selenium does is send the browser commands to do something or send requests for information. Most fo what we'll do with Selenum is a combination fo these basic commands:
python
from selenium import webdriver
//* Choose a driver based on the browser you're using
// driver = webdriver.Chrome()
// driver = webdriver.Firefox()
- Navigating to a web page
driver.get('url')
- There are a bunch of information you can request from a web browser(cookies, alerts, browser size/ position, etc)
title = driver.title
- Synchronizing the code with the current state of the browser is one of the biggest challenges with Selenium, and doing it well is an advanced topic.
- You want to make sure the element is on the page before you attempt to locate it and the element is in an interactable state before interacting with it.
- An implicit wait is rare we can use it:
driver.implicitly_wait(0.5)
- In selenium , we interact with elements, and you can't intercat with one without finding an element.
text_box = driver.find_element(by=By.NAME, value="my-text")
submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button")
// Locators — By.ID
, By.CLASS_NAME
, By.CSS_SELECTOR
, By.XPATH
, By.NAME
, By.TAG_NAME
, etc. Prefer CSS selectors where possible for speed & reliability.
text_box.send_keys("Selenium")
submit_button.click()
message = driver.find_element(by=By.Id, value="message")
text = message.text
driver.quit()
Most people use Selenium to execute automated tests for web applications, but Selenium supports any use case of browser automation.
Repetitive Tasks Perhaps you need to log into a website and download something, or submit a form. You can create a Selenium script to run with a service at preset times.
Web Scraping Are you looking to collect data from a site that doesn’t have an API? Selenium will let you do this, but please make sure you are familiar with the website’s terms of service as some websites do not permit it and others will even block Selenium.
Testing Running Selenium for testing requires making assertions on actions taken by Selenium. So a good assertion library is required. Additional features to provide structure for tests require use of Test Runner.