Skip to content

rawbil/selenium-doc

Repository files navigation

Selenium

  • 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 Tools

  1. Selenium Remote Control - Replaced by Webdriver
  2. Selenium Web Driver
  3. Selenium Grid - Allows running of tests on multiple machines.
  4. Selenium IDE - Involves installing the extension in your browser

Web Driver

  • 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.

Selenium IDE

  • Used to develop Selenium test cases
  • It records user's actions in the browser.

Selenuim Grid

  • 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.

Eight Basic Components

  • 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:

1. Start the Session

python

from selenium import webdriver
//* Choose a driver based on the browser you're using
// driver = webdriver.Chrome()
// driver  = webdriver.Firefox()

2. Take action on browser

  • Navigating to a web page
driver.get('url')

3. Request Browser Information

  • There are a bunch of information you can request from a web browser(cookies, alerts, browser size/ position, etc)
title = driver.title

4. Establish Waiting Strategy

  • 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)

5. Find an element

  • 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.

6. Take action on element

text_box.send_keys("Selenium")
submit_button.click()

7. Request element information

message = driver.find_element(by=By.Id, value="message")
text = message.text

8. End Session

driver.quit()

Common Uses

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages