Python: Selenium

Diwakar pratap
5 min readApr 7, 2022

--

Selenium is one among the foremost wide used open supply net UI (User Interface) automation testing suite. It was originally developed by Jason Sir William Huggins in 2004 as an interior tool at Thought Works. Se supports automation across completely different browsers, platforms and programming languages.

Selenium may be simply deployed on platforms like Windows, Linux, Solaris and Macintosh. Moreover, it supports OS (Operating System) for mobile applications like iOS, windows mobile and humanoid.

Selenium supports a range of programming languages through the employment of drivers specific to every language. Languages supported by Se embrace C#, Java, Perl, PHP, Python and Ruby. Currently, Se net driver is preferred with Java and C#. Se check scripts may be coded in any of the supported programming languages and might be run directly in most up-to-date net browsers. Browsers supported by Se embrace web somebody, Mozilla Firefox, Google Chrome and expedition.

Selenium are often wont to automatise practical checks and may be integrated with automation test tools like superstar, Jenkins, & dockhand to realize continuous testing. It can even be integrated with tools like TestNG, & JUnit for managing check cases and generating reports.

Selenium Features

· Selenium is associate open supply and transportable net testing Framework.
· Selenium IDE provides a playback and record feature for authoring checks while not the requirement to find out a test scripting language.
· It will be thought of because the leading cloud-based testing platform that helps testers to record their actions and export them as a reusable script with a simple-to-understand and easy-to-use interface.
· Selenium supports numerous operative systems, browsers and programming languages. Following is that the list:
· Programming Languages: C#, Java, Python, PHP, Ruby, Perl, and JavaScript
· Operating Systems: mechanical man, iOS, Windows, Linux, Mac, Solaris.
· Browsers: Google Chrome, Mozilla Firefox, web human, Edge, Opera, Safari, etc.
· It additionally supports parallel check execution that reduces time and will increase the potency of tests.
· Selenium will be integrated with frameworks like emmet and adept for ASCII text file compilation.
· Selenium also can be integrated with testing frameworks like TestNG for application testing and generating reports.
· Selenium needs fewer resources as compared to alternative automation check tools.
· WebDriver API has been indulged in Se whichis one in all the foremost vital modifications done to Se.
· Selenium net driver doesn’t need server installation, check scripts move directly with the browser.
· Selenium commands square measure categorised in terms of various categories that create it easier to know and implement.
· Selenium device (RC) in conjunction with WebDriver API is thought as Se two.0. This version was designed to support the colourful web content and Ajax.

Selenium Limitations

· Selenium doesn’t support automation testing for desktop applications.
· Selenium needs high talent sets so as to automatise tests a lot of effectively.
· Since Se is open supply software package, you’ve got to deem community forums to urge your technical problems resolved.
· We can’t perform automation tests on net services like SOAP or REST victimisation Se.
· We should understand a minimum of one amongst the supported programming languages to make tests scripts in Se WebDriver.
· It doesn’t have inherent Object Repository like UTF/QTP to keep up objects/elements in centralized location. However, we will overcome this limitation victimisation Page Object Model.
· Selenium doesn’t have any inherent reportingcapability; you’ve got to deem plug-ins like JUnit and TestNG for check reports.
· It is out of the question to perform testing on pictures. we’d like to integrate Se with Sikuli for image based mostly testing.
· Creating check setting in Se takes longer as compared to trafficker tools like UFT, RFT, Silk test, etc.
· No one is answerable for new options usage; they will or might not work properly.
· Selenium doesn’t offer any check tool integration for check Management.

Selenium WebDriver

Selenium WebDriver is that the most significant element of antioxidant Tool’s Suite. the newest unharness “Selenium a pair of.0” is integrated with WebDriver API that provides a less complicated and a lot of brief programming interface.

The following image can provide you with a good understanding of antioxidant elements and therefore the check Automation Tools.

WebDriver contains a constitutional implementation of Firefox driver (Gecko Driver). For different browsers, you would like to plug-in their browser specific drivers to speak and run the check. most ordinarily used WebDriver’s include:

· Google Chrome Driver
· Internet Explorer Driver
· Opera Driver
· Safari Driver
· HTML Unit Driver (a special headless driver)

Installing Python package for selenium

pip install selenium

Windows users

C:\Python39\Scripts\pip.exe install selenium

Drivers

Selenium requires a driver to interface with the chosen browser.
Links to some of the more popular browser drivers follow.

Chrome: https://sites.google.com/chromium.org/driver/

Edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

Firefox: https://github.com/mozilla/geckodriver/releases

Safari: https://webkit.org/blog/6900/webdriver-support-in-safari-10/

Navigating

WebDriver to navigate the link. The normal way to do this is by calling get method:

driver.get(“http://www.google.com")

Interacting with the page

<input type=”text” name=”passwd” id=”passwd-id” />

you could find it using any of:

element = driver.find_element_by_id(“passwd-id”)
element = driver.find_element_by_name(“passwd”)
element = driver.find_element_by_xpath(“//input[@id=’passwd-id’]”)
element = driver.find_element_by_css_selector(“input#passwd-id”)

To send data

element.send_keys(“some text”)
element.send_keys(“ and some”, Keys.ARROW_DOWN)

Filling in Forms

element = driver.find_element_by_xpath(“//select[@name=’name’]”)
all_options = element.find_elements_by_tag_name(“option”)
for option in all_options:
print(“Value is: %s” % option.get_attribute(“value”))
option.click()

To deal with DropDown menu

from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_name(‘name’))
select.select_by_index(index)
select.select_by_visible_text(“text”)
select.select_by_value(value)

Deselecting all the selected options

select = Select(driver.find_element_by_id(‘id’))
select.deselect_all()

Find the submit button and click it

# Assume the button has the ID “submit” :)
driver.find_element_by_id(“submit”).click()

Drag and drop

element = driver.find_element_by_name(“source”)
target = driver.find_element_by_name(“target”)
from selenium.webdriver import ActionChains
action_chains = ActionChains(driver)
action_chains.drag_and_drop(element, target).perform()

Moving between windows and frames

driver.switch_to_window(“windowName”)
driver.switch_to_frame(“frameName”)
driver.switch_to_frame(“frameName.0.child”)

Navigation: history and location

driver.forward()
driver.back()

Programming Example

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

driver=webdriver.Chrome(executable_path=”C:\\Users\\Diwakar\\Downloads\\chromedriver”)
driver.get(“https://www.google.com")
driver.maximize_window()

gtext=driver.find_element(By.NAME,’q’)
gtext.send_keys(“www.flipkart.com")
gtext.send_keys(Keys.ENTER)

flink=driver.find_element(By.CSS_SELECTOR,”body > div:nth-child(13) > div:nth-child(1) > div:nth-child(12) > div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > a:nth-child(1) > h3:nth-child(2)”)
flink.click()

fclose=driver.find_element(By.CSS_SELECTOR,”._2KpZ6l._2doB4z”)
fclose.click()

fsearch=driver.find_element(By.CLASS_NAME,”_3704LK”)
product=input(“Enter the product you want to search: “)
#product=”Smartwatch”
fsearch.send_keys(product)
fsearch.send_keys(Keys.ENTER)
time.sleep(3)

#driver.execute_script(“window.scroll(0,2100);”)
elem=driver.find_element(By.CSS_SELECTOR,”body > div:nth-child(1) > div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(2) > div:nth-child(9) > div:nth-child(1)”)
elem.click()

Output

--

--

No responses yet