Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Selenium Basics

24 May 2022 10:55

Features of Selenium
-----------------------------
Selenium is a Cloud-Based Testing Platform
Supports Parallel Execution
Requires fewer resources

Install Python
Install selenium by
Pip install -U selenium
Configuring Pycharm with selenium
1. Click on file
2. Click on settings
3. Click on the project we have created
4. Click on Python Interpreter
5. See whether selenium package is available or not
6. If not click on plus symbol
7. Now search for selenium
8. Click on install package

Note:
---------
Configuration should be done for every new project

Note:
-------
Pycharm also gives u one separate Interpreter
Whatever python packages are present in local python place
So give project interpreter as local python path not ur pycharm python interpreter

For that create one python file in ur new project


Bottom u can see python interpreter click on it
Change the python interpreter path to ur local python Interpreter

Process of Creating the Browser Objects and interacting browser with sel:
------------------------------------------------------------------------------------------------------
from selenium import webdriver

#Every Browser Exposes an Executable file


#Through selenium test we need to invoke the executable file which will invoke the actual Browser
#now we need to give executable path to Chromeclass

#https://chromedriver.chromium.org/
#Above mentioned one is the link to download the ChromeDrivers for selenium

#Note
'''Install the Drivers of the versions same as that of ur chromebrower versions
We get the versions by clicking on the help of the browser'''

driver=webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://www.facebook.com")#get will help u to hit the url on browser

print(driver.title)#it is used for getting the title of the Webpage

print(driver.current_url) #prints the currenturl and we can know our site is hacked or not

driver.get("https://www.facebook.com/login/")

driver.minimize_window() #it minimises the window


driver.back() #it takes the browser to previous opened page

driver.refresh() #it refreshes the opened page


driver.close() #it closes the opened browser

Note:
-------
Selenium Code will be same even we r using the different browsers
Just change executable_path value as firefox driver

# Link for downloading FireFox


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

Example of firefox:
------------------------
driver=webdriver.Firefox(executable_path="C:\\geckodriver.exe")

# Link for IE
--------------
https://www.selenium.dev/downloads/

Example IE Driver
-----------------------

driver=webdriver.Ie(executable_path="C:\\IEDriverServer.exe")

How to tell selenium exactly go specified text box and write some data
----------------------------------------------------------------------------------------------
By Locators we can do that

The different locators in Selenium are as follows:


• By CSS ID: find_element_by_id
• By CSS class name: find_element_by_class_name
• By name attribute: find_element_by_name
• By DOM structure or xpath: find_element_by_xpath
• By link text: find_element_by_link_text
• By partial link text: find_element_by_partial_link_text
• By HTML tag name: find_element_by_tag_name

# for text box we cant use id bcoz they will not give id so we give name

First of all we need to check what they specified like name or id or something like that

For Textbox we need to write something so we use send_keys('Value')

For Check Box as we need to select that we use click() method

Xpath and CSS are independent of what the developers have defined

Whatever the properties developer has defined we can get the paths by using CSS or X path

# Note:

Customised CSS selector Syntax:


--------------------------------------------

Tagname[attributeName='Value']

Example:
-----------

Input[name='name']

Process to check the given css selector is correct or nor follow this process
----------------------------------------------------------------------------------------------------

1. Go To Console and run below command

2. $("Input[name='name']")

Note:
---------

If u r Css selector matches more than one Text Box ,,as flow executes from top to bottom
First occurance will be given preference

Customised Xpath selector Syntax:


--------------------------------------------

//tagname[@attributeName='Value']

Example:
-----------

//Input[@type='submit']

Process to check the given css selector is correct or nor follow this process
----------------------------------------------------------------------------------------------------

1. Go To Console and run below command

1. $x("//Input[@type='submit']")

Example Demonstrating the different Locators


---------------------------------------------------------------

From selenium import webdriver

driver=webdriver.Chrome(executable_path="C:\\Users\\Jspiders\\Desktop\\harshad\\chromedriver.exe")

driver.get("https://rahulshettyacademy.com/angularpractice/")

#these are by using Name and id


'''
driver.find_element_by_name("name").send_keys('Harshad')
driver.find_element_by_name("email").send_keys("Vali")
driver.find_element_by_id("exampleCheck1").click()
'''
#Now we are Dealing with the CSS and xpath locators

driver.find_element_by_css_selector("input[name='name']").send_keys("Harshad")
driver.find_element_by_xpath("//input[@type='submit']").click()

Extracting the text from the element and printing the same

print(driver.find_element_by_class_name('alert-success').text)

# Representation of Regex CSS selectors


TagName[attributeName*='Value']

# Representation of Regex Xpath selectors

//TagName[contains(@attributeName,'Value']

Creating CSS by using ID:


----------------------------------
TagName[#IdName]

Creating CSS by using ClassName:


----------------------------------
TagName[.ClassName]

fromseleniumimportwebdriver

driver=webdriver.Chrome(executable_path="C:\\Users\\Jspiders\\Desktop\\harshad\\chromedriver.exe")

driver.get("https://login.salesforce.com/")
driver.find_element_by_css_selector("#username").send_keys('Harshad')#By using Id
driver.find_element_by_css_selector(".password").send_keys("Nikky")#By Using Class
driver.find_element_by_css_selector(".password").clear()# It clears The Data Whatever u have Given in the TextBox

Find_element_by_link_text:
----------------------------------------
1. This is used whenever we are dealing with the anchor(Hyper Links)
2. It is used to find the element by using the text mentioned in the HyperLink

driver.find_element_by_link_text("ForgotYourPassword?").click()
driver.find_element_by_partial_link_text("ForgotYour").click()

Generating Xpath by using Text:


------------------------------------------

"//TagName[text()='Value']"

Driver.find_element_by_xpath("//a[text()='Cancel']")

It is very cool feature but use it whenever u deal with constant text which will not be changed over the time
Generating Xpath and CSS by using Traversing through Tags:
--------------------------------------------------------------------------------

It is used when u r not sure that none of attributes will give the unique Identifier

By using Xpath
-------------------
parentTag/childtag

Example:
------------

//ParentTagName[@attribute='value']/childTagname

Example to Traverse form Grandfather to Child


--------------------------------------------------------------

//form[@name='login']/div[1]/label

GrandFather/father/child

By using CSS
-------------------
parentTag childtag

Example:
------------

ParentTagName[attribute='value'] childTagname

Dealing with DropDown


---------------------------------

Inorder to deal with dropdowns we need to first import Select class

From selenium.webdriver.support.ui import Select

Incase of Drop down lists two types we have

1. Static DropDown(content will be constant)


2. Dynamic DropDown(content will change Dynamycally)

#select class provide the methods to handle the options in dropdown


Dropdown=Select(driver.find_element_by_id="IDname")# object is created

Note:
-------
Select class can be used when tagname is only select tag not for other Tags
------- -------

Assertions:
---------------

assert keyword is used for raising the AssertionError if the specified condition is False

Note:
---------

Ultimately we use assert to check whether the testcase is passing or Failing

assert Condition

If condition returns True --->Testcase is pass


If condition returns False---->Test case is Failed

You might also like