Different Browsers Execution with Selenium

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

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