Selenium is an open-source tool that automates browsers. It’s widely used in web testing to simulate user interaction with a website across different browsers
- Selenium Features
- Selenium Installation
- Selenium Locators
- XPath Fundamentals
- CSS Selectors Methods
- Different Browsers Execution
- find_element & find_elements
- Check Enabled Status
- Check Displayed Status
- Check Selected Status
- Selenium Waits
- Send_keys Method
- Click Method
- Get Text
- Get Attribute Value
- Get Current URL
- Forward, Back, Refresh
- Take Screenshot
- Handle Browser Tabs
- Handle iframe
- Mouse Hover
- Context-Click
- Drag & Drop
- Handle Alerts
- Handle Dropdown
- Execute Javascript
- Scroll To element
- Headless Mode Execution
- Chrome Options
- Keyboard Action
Introduction to Selenium and Browser Automation
Selenium is an open-source tool that automates browsers. It’s widely used in web testing to simulate user interaction with a website across different browsers, ensuring that it functions correctly. From clicking buttons to filling out forms, Selenium makes it easy for developers and testers to automate these tasks using different browsers. This capability is vital for cross-browser compatibility testing to verify that a website works smoothly across various platforms.
With Python being one of the most popular programming languages, combining Selenium with Python provides a simple, yet powerful, solution for automating browsers. Let’s explore how you can execute Selenium scripts across different browsers using Python.
Popular Browsers Supported by Selenium
Selenium supports several browsers, ensuring you can automate tasks across multiple platforms. Here are some popular browsers supported:
- Google Chrome: The most widely used browser, known for its speed and reliability.
- Mozilla Firefox: Open-source, secure, and highly customizable.
- Microsoft Edge: Built on Chromium, Microsoft’s modern browser.
- Safari: Apple’s browser, mainly used on macOS and iOS.
Running Automation Scripts in Chrome Browser
Here’s an example of launching a website and interacting with it:
from selenium import webdriver
from selenium.webdriver.common.by import By
# Launch chrome browser
driver = webdriver.Chrome()
driver.implicitly_wait(20)
driver.maximize_window()
# Open google page on the browser
driver.get('https://www.google.co.in')
# Send text to google search field
driver.find_element(By.NAME, 'q').send_keys('Selenium automation')
# Click on search button
driver.find_element(By.NAME, 'btnK').click()
# Close browser
driver.close()
Running Automation Scripts in Firefox Browser
from selenium import webdriver
from selenium.webdriver.common.by import By
# Launch Firefox browser
driver = webdriver.Firefox()
driver.implicitly_wait(20)
driver.maximize_window()
# Open google page on the browser
driver.get('https://www.google.co.in')
# Send text to google search field
driver.find_element(By.NAME, 'q').send_keys('Selenium automation')
# Click on search button
driver.find_element(By.NAME, 'btnK').click()
# Close browser
driver.close()
Running Automation Scripts in Edge Browser
from selenium import webdriver
from selenium.webdriver.common.by import By
# Launch Edge browser
driver = webdriver.Edge()
driver.implicitly_wait(20)
driver.maximize_window()
# Open google page on the browser
driver.get('https://www.google.co.in')
# Send text to google search field
driver.find_element(By.NAME, 'q').send_keys('Selenium automation')
# Click on search button
driver.find_element(By.NAME, 'btnK').click()
# Close browser
driver.close()
Combine Scripts to run with all Browser
from selenium import webdriver
from selenium.webdriver.common.by import By
browser_name = 'Chrome'
driver = None
"""
Launch different browser as per
the value of browser_name variable
"""
if browser_name.lower() == 'chrome':
driver = webdriver.Chrome()
elif browser_name.lower() == 'firefox':
driver = webdriver.Firefox()
elif browser_name.lower() == 'Edge':
driver = webdriver.Edge()
driver.implicitly_wait(20)
driver.maximize_window()
# Open google page on the browser
driver.get('https://www.google.co.in')
# Send text to google search field
driver.find_element(By.NAME, 'q').send_keys('Selenium automation')
# Click on search button
driver.find_element(By.NAME, 'btnK').click()
# Close browser
driver.close()