Selenium is an open-source tool used for automating web browsers. It is used widely for automating web applications for testing purposes but can also be used in other scenarios like web scraping.
from selenium import webdriver # create a new Chrome browser driver = webdriver.Chrome() # open a website driver.get("https://www.example.com") # perform actions on the website # ... # close the browser driver.quit()
Let's say we want to automate the process of logging in to a website. We can use Selenium to fill in the login information and click the login button for us.
from selenium import webdriver from selenium.webdriver.common.keys import Keys # create a new Chrome browser driver = webdriver.Chrome() # open the website driver.get("https://www.example.com/login") # find the email input field and fill it in email_input = driver.find_element_by_name("email") email_input.send_keys("firstname.lastname@example.org") # find the password input field and fill it in password_input = driver.find_element_by_name("password") password_input.send_keys("password123") # find the login button and click it login_button = driver.find_element_by_css_selector(".btn-login") login_button.click() # close the browser driver.quit()
The output of the example would be:
- A new Chrome browser opens up
- The login page of the website is loaded
- The email input field is filled in with "email@example.com"
- The password input field is filled in with "password123"
- The login button is clicked
- The browser closes
Selenium works by automating web browsers. It allows us to interact with web pages just like a user would, but programmatically.
In the example above, we first import the necessary modules from Selenium. We then create a new
webdriver.Chrome() object which creates a new chrome browser window.
We then use
driver.get() to navigate to the login page of our website. We can then use various methods to interact with the web page, like
send_keys() to fill in the email and password input fields.
Once the login information is filled in, we can use
driver.find_element_by_css_selector() to locate the login button and
click() to simulate clicking it. Finally, we close the browser using
Selenium is commonly used for automated testing of web applications. It allows us to simulate user interactions like clicking buttons and filling in forms, which helps us test the functionality of our web application.
It can also be used for web scraping, automated form filling, and other tasks that involve interacting with web pages.
- You must have the appropriate web driver installed for your browser (e.g., Chrome driver for Chrome browser).
- When using WebDriver with a language like Python, it is important to clean up the WebDriver object after usage to prevent memory leaks.
Selenium is a powerful tool for automating web browsers. It allows us to simulate user interactions on web pages, which is useful for automated testing, web scraping, and other tasks involving web pages. To use it, we need to create a new
webdriver object, navigate to the page we want, and use various methods to interact with the page. Once we're done, we need to clean up the WebDriver object by calling