0% found this document useful (0 votes)
2 views

Selenium

The document provides a comprehensive overview of Selenium, an open-source automation testing tool for web applications. It covers the advantages of automation testing, types of Selenium, supported browsers, and installation steps, along with detailed explanations of various methods and locators used in Selenium. Additionally, it includes information on Java archive files, Selenium architecture, and practical examples for using Selenium in testing web applications.

Uploaded by

sai vivek
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Selenium

The document provides a comprehensive overview of Selenium, an open-source automation testing tool for web applications. It covers the advantages of automation testing, types of Selenium, supported browsers, and installation steps, along with detailed explanations of various methods and locators used in Selenium. Additionally, it includes information on Java archive files, Selenium architecture, and practical examples for using Selenium in testing web applications.

Uploaded by

sai vivek
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Selenium

Automation Testing
 Automation Testing is an automatic technique where tester write the script using the
automation tool to execute test cases and test the software.
 We can also say that automation testing is used to automate the respective tasks which are
difficult to perform manually
 The main goal of automation testing is to increase the test efficiency and develop a quality of
software.
 Converting manually test cases into automation test script is called automation testing.

Advantages
 It increases the test execution speed
 It helps in the maximum coverage of test cases.
 It saves the time and cost while compare to manually testing.
 We can re-use the test scripts
 It maintains good consistency
 It improves quality of the software.

Selenium Tool
Selenium is free & open-source web application automation tool (web application functional testing
automation testing tool)

Why selenium is popular


 Selenium is open source and we can see the source code
 Selenium is a free tool
 Selenium is handled by google
 Selenium is a Web Application functional testing tool

What kind of application we can automate?


 We can automate only web applications

What are the flavours of selenium or different types of selenium?


1. Selenium Core
2. Selenium RC (Remote Control)
3. Selenium Grid
4. Selenium IDE (Integration Development Environment)
5. Selenium WebDriver
6. Selendroid
7. Appium
8. Winium... etc

What are the automation testing tools?


Selenium, coded UI, new load, JMeter, load runner, acunetics QTP (Quick test professional)

What are the browsers supported by selenium?


Google chrome, Mozilla Firefox, internet explorer, Microsoft edge, safari, opera, html, unit phantom
js
What are .jar files & Why we need .jar files
Jar means java archive.

 It is a file based on zip file format where many files are converted into one single file known
as jar files.
 We need jar files to import the properties based on the requirement

How we can create the jar files


1. Open eclipse
2. Create java project
3. Right click on the project and click export
4. Select java folder and click on jar
5. Place the jar file into the appropriate folder

How to import the properties based on the requirement


There are two ways to import the requirement:

1. Internally
a. Right click on the project and select properties option
b. Select java build path & click on libraries
c. Select classpath and click on add jars and go to the project and select the jar files
d. Next click on apply and close
2. Externally
a. Right click on the project and select properties option
b. Select java build path & click on libraries
c. Select classpath and click on add external jars and go to the appropriate location and
click on open
d. Next click on apply and close

Selenium Architecture

S Java LB Selenium Chrome Chrome Web


server driver applications
S C# LB
Gecko
etc Firefox
driver

Client Binding Server Driver


Browser Application
Or Executable Under Test
.jar Files
.exe Files (AUT)
Language Bindings
 Selenium supports 14 different coding languages like java, phyton, C#, etc.
 This are called as language binding or client bindings
 This client bindings communicate with selenium server. Then the server performs the action
on the browser with the help of driver executables. In order to perform the action, it uses
JSON Wire protocol (Java Script Object Notation).
 Since selenium server internally contains selenium java client binding also, hence while
installing selenium we use only server.jar file & driver executables.

Steps to install selenium


1. Download selenium server.jar file & browser specific driver executable file in the download
page of selenium (www.selenium.dev/downloads)
2. Extract the driver executable file (Unzip).
3. In eclipse create a new java project and create two new folders as driver & jar inside the java
project.
4. Copy & Paste driver file into the driver folder
5. Similarly copy & paste selenium jar file into the jar folder.
6. Add jar file to the build path, so that we can use import statement to import the required
class of selenium.

NOTE

Don’t add driver.exe file to build path because eclipse will not allow us to add .exe file to the build
path.

1. To specify the path either we add it manually to environment variable or we should add it
programmatically using System.setProperty(key, value) statement.
2. While specifying the path we use relative path by specifying the “.” operator at the
beginning which represent current java project
3. We should set the path before opening the browser only, else we will get illegal state
exception.
4. For easy maintenance we set the path of all driver executables in static block as shown
below:
Ex:
Architecture for WebDriver/Selenium Java Language
Binding

 The super most interface is search context which is extended by Web Driver interface.
 Web Driver interface is implemented in Remote Web Driver class.
 Remote Web Driver class implements other interfaces also, such as JavaScriptExecutor,
TakeScreenShot, etc.
 All the browser specific classes such as Chrome driver, Firefox driver & Internet explorer
driver, etc extends Remote Web Driver class.

Steps to use filter in eclipse


Eclipse will display methods of object class when we press “.” after any obj_ref variable. To hide it:

1. Go to window tab in eclipse


2. Next go to java
3. Next go to Appearance
4. Select Type Filters
5. Select an option called Add and type fully qualified name of object class “java.lang.Object”
6. Click on apply and close

Methods of Search Context


1. findElement(By by) : WebElement Return Type
2. findElements(By by) : List<WebElement>

Methods of Web Driver


1. close()
2. get(String url)
3. getCurrentURL()
4. getPageSource()
5. getTitle()
6. getWindowHandle()
7. getWindowHandles()
8. manage()
9. navigate()
10. quit()
11. switchTo()

Methods of Java Script Executor


1. executeAsyncScript(String script,Object
2. executeScript(ScriptKey)

Methods of Take Screen Shot


1. getScreenshotAs(OutputType<x> target)

Methods Web Element


1. clear()
2. click()
3. getAttribute(String name)
4. getCssValue()
5. getLocation()
6. getRect()
7. getSize()
8. getTagName()
9. getText()
10. isDisplayed()
11. isEnabled()
12. isSelected()
13. sendKey()
14. submit()

What is Up casting? Is it used in selenium and why?


Converting the sub-class object into super class type is called as up-casting
Ex: WebDriver driver=new ChromeDriver();
In selenium we use up-casting so that we can execute the same script on any browser
Explain WebDriver driver=new ChromeDriver() ?
 WebDriver is an Interface
 driver is a reference variable
 “=” is an assignment operator
 new is a keyword(used to create an object)
 ChromeDriver() is a constructor
 “;” is used to end the statement or statement delimiter.

How to enter URL without using get() method ?


 By using navigate().to(“URL”);

Difference between get() and navigate()?


get() method is used to enter the URL only, whereas by using navigate we can enter the URL, click on
backward, forward & refresh also.

Ex:

Difference between get() and to()?


There is no difference between get() and to() method because to() method internally calls get()
method, both are used to enter URL.

How to close a browser without using close() method?


By using quit() method

Difference between close() & quit()?


close() method will closes the current browser, whereas quit() method will close all the browsers
(including child browsers).
How do you maximize the browser?
By using driver.manage().window().maximise()

How to delete all the cookies in the browser?


driver.manage().deleteAllCookies();

Ex:

getPageSource()
Write a script to print HTML source code of a webpage

Web Element
 Anything present on the web page is called as web element, such as button, link, text box…
etc.
 Web element are created using HTML language (Hyper Text Markup Language)
 Each HTML contains three things:
o Tag (tag name)
o Attribute
 Attribute name
 Attribute value)
o Text (visible text)
Ex: HTML code for login button
<div id=”d1”> Login </div>
 In the above HTML code div is the tag name, id is the Attribute name, d1 is
Attribute value & login is the visible text
 In order to see the HTML code of the required element; right click on the element
and select the option inspect, if right click is disabled on the text then press
Ctrl+Shift+i or F12 key. It will display developer’s tool bar
 In order to inspect another element click on inspect button and then click on
required element.

NOTE

In selenium before performing any action such as clicking, typing & selecting, etc. we should find the
element using locators

Locators
Locators are used to find the elements, in selenium there are 8 type of locators & all are static
method present in By class.

1. tagName()
2. id()
3. name()
4. className()
5. linkText()
6. partialLinkText()
7. cssSelector()
8. xpath()

By is an abstract class and all the locators will take string as an argument

Working procedure
HTML code for static webpage is:

<html>
<body>
<a id=”d1” name=”n1” class=”c1” href=https://www.qspiders.com/>Google</a><br>
<a id=”d1” name=”n1” class=”c1” href=https://www.qspiders.com/>Google</a><br>
</body>
</html>

NOTE

Type the above HTML code in the notepad and save it as demo.html on the desktop, inorder to
create a static webpage.
Write a script to click on Google link by using tag name locator

Interview Questions

1. What is the return type of findElement()


Ans: WebEelement
2. What is the argument accepted by findElement()
Ans: By type
3. If the specified locator is not matching with any of the elements, then what findElement()
does?
Ans: It will throw NoSuchElementException
4. If the specified locator is matching with multiple elements, then what findElement() does?
Ans: It takes the first matching element or it returns the address of first matching element.

Write a script to click on google link by using tagName(), id(), name(), className()

LinkText & Partial LinkText


Both LinkText & Partial LinkText locators can be used to find the link. If we try it on any other type of
element, we get NoSuchElement Exception.

Syntax: driver.findElement(By.LinkText(“Google”)).click;

NOTE

If the text of the link is changing partially we can use Partial LinkText.

Ex:

HTML Code

<a…>Inbox(7)</a>

<a…>Inbox(7)</a>
Syntax: driver.findElement(By.PartialLinkText(“Inbox”)).click();

Limitation of Partial LinkText


 Element should be a link
Ex: <span>Inbox</span> (Here we cannot use Partial LinkText)
 Text should be partially changing
Ex:
<span>7</span>
<a>7</a>

CSS Selector
 CSS stands for Cascading Style Sheet
 CSS selector is one of the locator in selenium
Syntax: tag[Attribute_Name=’Attribute_Value’]
Ex:
a[id=’d1’]
a[name=’n1’]
a[class=’c1’]
a[href=’https://www.google.com’]
These are called as CSS Expression

NOTE

We can check the CSS expression in the browser by using following steps:

1. Inspect any element


2. Press ctrl+F
3. Type the CSS Expression

If we get;
1 of 1 -> One matching Elements
1 of 3 -> Multiple matching elements
0 of 0 -> No matching elements.

X-Path
 It is path of the element in a HTML tree
 While writing X-Path it starts with “.” It also represents current webpage or HTML
document, using “.” Is not mandatory
 To navigate from parent to child element (tag) we use “/” (Single Backward Slash)
Ex: ./HTML/body/a

Type:

 Absolute X-Path
 Relative X-Path
1. X-Path by Attributes
2. X-Path by Text Function
3. X-Path by Contains Function
4. Traversing X-Path
5. Independent-Dependent X-path
6. X-Path by group Index

Absolute X-Path
Starting from HTML to desired element/required element is called absolute X-path.

X-Path Index
In X-Path we can use index which starts from 1, if there is another element under the same parent
with the same tag name then the index becomes 2 and so on....

Ex:

Ex:

HTML

Body

1. div
1. a link 1
2. a link 2
2. div
1. a link 3
1. img img
2. a link 4

Questions

1. Write a X-Path for link1

/HTML/body/div[1]/a[1]

2. Write a X-Path for link2

/HTML/body/div[1]/a[2]

3. Write a X-Path for link3

/HTML/body/div[2]/a[1]

4. Write a X-Path for link4

/HTML/body/div[2]/a[2]

5. Write a X-Path for link 1 & 3


/HTML/body/div/a[1]
6. Write a X-Path for link 2 & 4
/HTML/body/div/a[2]
7. Write a X-Path for link 1 & 2
/HTML/body/div[1]/a
8. Write a X-Path for link 3 & 4
/HTML/body/div[2]/a
9. Write a X-Path for link 1,2,3 & 4
/HTML/body/div/a
10. Write a X-Path for link img
/HTML/body/div[2]/img
Or
/HTML/body/div/img
11. Write a X-Path for link 3,4 & img
/HTML/body/div[2]

All the above X-Path is called as absolute X-Path.

Relative X-Path
Relative X-Path starts with “//” which represents descendent (Child, grand child, great grand child,...,
etc)

Ex:

HTML

Body

3. div
1. a link 1
2. a link 2
4. div
1. a link 3
3. img img
4. a link 4

Questions

1. Write a X-Path for link1

//div[1]/a[1]

2. Write a X-Path for link2

//div[1]/a[2]

3. Write a X-Path for link3

//div[2]/a[1]

4. Write a X-Path for link4

//div[2]/a[2]

5. Write a X-Path for link 1 & 3

//div/a[1]

6. Write a X-Path for link 2 & 4

//div/a[2]
7. Write a X-Path for link 1 & 2

//div[1]/a

8. Write a X-Path for link 3 & 4

//div[2]/a

9. Write a X-Path for link 1,2,3 & 4

//a or //div/a

10. Write a X-Path for link img

//img

Or

//div/img

11. Write a X-Path for link 3,4 & img


//div[2]

The above X-Path are called as relative X-Path

/ //
It represents child It represents descendent (Child, grand child, great grandchild)
//a //table//a
It matches with all the links which are present It matches with all the links which are present
in anywhere of the webpage inside the table

Questions

Write an X-Path to identify all the links & all the images

//a|//img

Write an X-Path to identify all the link1 & link4

//div[1]//a[1]|//div[2]//a[2]

Write an X-Path to identify all the link1 & link4

//div[1]//a[2]|//div[2]//a[1]

X-Path by Attribute
We can also use attribute of the element while writing X-Path.

Syntax:

//tag_name[@Attribute_name = ‘Attribute_value’]
And Condition
Syntax:

//tag_name[@Attribute_name1 = ‘Attribute_value1’ and @Attribute_name2 = ‘Attribute_value2’]

OR Condition
Syntax:

//tag_name[@Attribute_name1 = ‘Attribute_value1’ or @Attribute_name2 = ‘Attribute_value2’]

Not Condition
Syntax:

//tag_name[not(@Attribute_name1 = ‘Attribute_value1’ )]

Questions

HTML Code

A1: <input type=”text” value=”A”/> <br>

B1: <input type=”text” value=”B”/> <br>

A2: <input type=”button” value=”A”/> <br>

B1: <input type=”button” value=”B”/> <br>

C1: <input type=”checkbox” checked/> <br>

C2: <input type=”checkbox”/> <br>

Write an X-Path to identify the following elements

 All the text box


//input[@type=’text’]
 All the buttons
//input[@type=’button’]
 All the checkbox
//input[@type=’checkbox’]
 All the text box & all the buttons
//input[@type=’text’ or @type=’button’]

 Only the 1st text box


//input[@type=’text’ and @value=’A’]
 Only the 2nd button
//input[@type=’button’ and @value=’B’]
 1st text box & 1st button
//input[@value=’A’]
 Only selected checkbox
//input[checked]
 Only Un-Selected checkbox
//input[@type=’checkbox’ and not(@value=’checked’)]

X-Path by text function


In X-Path we can also use text

Syntax: //tag_name[text()=’value’]

Ex1:

HTML Code

<div>Login</div>

X-Path: //div[text()=’Login’]

Ex2:

HTML

<td id \=”header Container” class=”header”>Please identify yourself</td>

X-Path: //td[text()=’Please identify yourself’]

Or

X-Path: //td[.=’Please identify yourself’]

X-Path by Contains
If the text value is partially changing then we can use contains function in the X-Path.

Syntax: //tag_name[contains(text(),’value’]

Ex:

HTML Code

<nobr>actiTime 2020 Online</nobr>

X-Path:

//nobr[contains(text(),’actiTime’)]

How do you identify element without using partial link text?

By using X-Path by contains function

HTML Code

<a….>Inbox(7)</a>

X-Path
//a[contains(text(),’Inbox’)]

Or

//a[contains(.,’Inbox’)]

NOTE

We can also use contains function for attribute

Syntax:

//tag[contains(@Attribute_name, ‘Attribute_value’)]

Ex:

HTML Code

<img class=”fb_logo_8ilh img” src=https://static.xx.fbcdn.net/rsrc.php/y8/r/dF5S1D3UHWd.svg>

X-Path

//img[contains(@class,’logo’)]

Or

//img[contains(@src,’fbcdn’)]

Traversing X-Path
Navigating from one element to another element is called as traversing

Type:

1. Forward Traversing
Navigating from parent element to child is called as Forward traversing
2. Backward Traversing
Navigating from child element to parent element is called backward traversing.
Ex:

<table border=”1”>
<tr>
<td>Java</td>
<td>100</td>
</tr>
<tr>
<td>Selenium</td>
<td>300</td>
</tr>
</table>

HTML Tree tr
HTML

body td td
table
IDE java 100 DE
tbody

1. tr
1. td Java
2. td 100 Java 100
2. tr Selenium 300
1. td Selenium
2. td 300

X-Path for Forward Traversing (From HTML to Selenium)


/HTML/body/table/tbody/tr[2]/td[1]
or
//tr[2]/td[1]
or
//td[text()=’Selenium’]

X-Path for Backward Traversing (From Selenium to HTML)


/HTML/body/table/tbody/tr[2]/td[1]/../../../../..
or
//tr[2]/td[1] /../../../../..
or
//td[text()=’Selenium’] /../../../../..
Independent-Dependent X-Path
 If the value is changing completely then we handle it using independent-dependent x-path.
Here we write the x-path by using backward & forward traversing.
 We always start from independent element and end with dependent element.

Ex:

div

span div

div div

a a

samsung 13,999
IDE DE
X-Path:

//a[text()=’samsung’]/../../../div/div/a

Steps to construct HTML Tree & Write X-Path

1. Identify the independent & dependent element.


2. Inspect independent element & write the x-path by looking at the HTML code.
3. Move the mouse pointer in the upward direction step-by-step till it highlights both
independent & dependent element, it will be common parent.
4. Add x-path of common parent to the above x-path.
5. Use down arrow key to navigate from common parent to dependent element.
6. Add it’s path next to the common parent.

Assignment

1. Write an X-Path to identify the status of testing present in type of work web table under
settings of actiTime application
 X-Path:
2. Write an X-Path to identify set by default link for support type of work
X-Path: //a[text()=’support’]/../../td[5]/a
3. Write an X-Path to identify the price (rate) of engineering type of work
X-Path: //a[text()=’engineering’]/../../td[4]/span
4. Write an X-Path to identify to identify the stable version of ruby language present in client
bindings of selenium download page.
X-Path: //p[text()=’Ruby’]/../p[2]/a
OR
//p[text()=’Ruby’]/../p[contains(text(),’Stable:’]/a
5. Write an X-Path to identify API docx link for phyton language present in client binding of
selenium download page.
 X-Path: //p[text()='Python']/../
p[4]/a[@href="https://seleniumhq.github.io/selenium/docs/api/py/index.html"]

X-Path by group index


While writing X-Path expression we can write the expression within the braces & we can specify the
index outside the braces which is called as group index

Ex:

HTML
A
body
B
div C
a A D

a B X-Path: (//a)[1] -> A


div

a C

a D

 When we use group index 1st it will execute the expression present inside the braces, in this
example 1st it will execute ‘//a’ & it will store matching element (A, B, C, D) links into X-Path
array where index starts from 1.
 Then it will identify the matching element based on the index which is specified outside the
bracket. In this example it matches with 1st link “A”.
 //a -> A,B,C,D
 (//a)[1] -> A Here it matches with only 1st link
 (//a) [2] -> B
 (//a) [3] -> C
 (//a) [4] -> D
 (//a) [last()] -> D
 //a[1] -> AC Here it matches with all the links having index 1
 (//a[1]) [1] -> A
 (//a[1]) [2] -> B
 //a[2] -> BD
 (//a[2]) [1] -> B
 (//a[2]) [2] -> D
 (//a)[1]|(//a)[last()] -> AD Here it matches with 1st & last link
 //a[3]
No matches
 (//a)[5]
Ex2:

HTML Code

<table border=”1”>
<tr>
<td> Selenium </td>
<td>100</td>
</tr>
<tr>
<td>Selenium</td>
<td>300</td>
</tr>
</table>

Write an X-Path to identify the price of 2nd selenium book


(//td[text()=’selenium’])[2]/../td[2]

HTML Tree
HTML

body
Selenium 100
table
Selenium 300
tbody

1. tr
3. td Selenium
4. td 100
2. tr
5. td Selenium
6. td 300

Assignment

1. Write an X-Path to identify the price of “Roadster T-Shirt” (1st T-Shirt) present under men’s
section of myntra.com
 (//h3[text()='Roadster'])[1]/../div/span[1]/span[1]
2. Write an X-Path to identify the price of “Libas Kurtas” present under women’s section of
myntra.com
 (//h3[text()='Libas'])[1]/../div/span[1]/span[1]
3. Write an X-Path to identify the price of “H&M Jeans” present under kid’s section of
myntra.com
 (//h3[text()='H&M'])[1]/../div/span[1]/span[1]
4. Write an X-Path to identify the price of “OnePlus 9r” (1st mobile) present in Amazon.in
 (//span[contains(text(),'OnePlus 9R
5G')])[1]/../../../../div[3]/div[1]/div/div[1]/div[2]/a/span/span[1]
or
(//span[contains(text(),'OnePlus 9R 5G')])[1]/../../../..//span[@class=’a-offscreen’]
5. Write an X-Path to identify the price of “Mi Band 5” present in Flipkart.com
 //div[contains(text(),'Mi')]/../../div[2]/div[1]/div[1]/div[1]
or
//div[contains(text(),'Mi')]/../..//div[@class=’_30jeq3_1_WHN1’]

What are the frequently used locators in selenium?

1. id
2. name
3. link text
4. x-path

Clear()
Write a script to remove the text present in email text box of open source billing app?

Assignment

1. Write a script to actiTime application?

URL: https://demo.actitime.com

UN: admin

PW: manager

Hint: use SendKeys(“admin”) & SendKeys(“manager”) to enter the data into the field
isDisplayed()
Write a script to check whether Facebook logo is displayed or not?

is.Enabled()
Write a script to check login button of Facebook is enabled or not?

is.Selected()
Write a script to print the status of checkbox present in actiTime application
getSize()
Write a script to find the height & width of email text box present in Facebook

Output

Height= 52
Width= 364

NOTE

Return type of getSize() is Dimension(class)

Assignment

Write a script to check whether height & width of user name & password text box are equal or not
of actiTime application
getLocation()
Write a script to check whether username & password text box are properly aligned or not.

Assignment

1. Write a script to check whether gender radio buttons are properly aligned or not present in
facebook.com after clicking create new account button [Hint: Use Thread.sleep() after clicking
the button]

NOTE:

Return type of getLocation() is Point(class).


submit()
Write a script to click on submit button present in open-source billing app without using click
method.

By using submit() method. We can use submit() only if type=’submit’ attribute is present.

Keys()
Write a script to copy the text present in email text box and paste it into the password textbox
present in open-source billing app.

getText()
Write a script to print the text of forgot your password link present in actiTime application.
getTagName()
Write a script to check whether the tag name of the actiTime link is anchor tag(a) or not

getAttribute()
Write a script to print the URL of actiTime link
switchTo()
Syntax: obj_ref_switchTo().activeElement();

Write a script to find active element present in www.google.com and type java where the active
element is present.

if search box is not an active element, then we need to use:

driver.findElement(By.id(“id”)).sendKeys(“java”);

getCurrentURL()
Write a script to check whether www.seleniumhq.org link is navigating to www.selenium.dev or
not

Here we can use equals() method


also but we need to mention the full
URL
Assignment

Automate the following scenario:


1. Open the browser
2. Go to the URL https://www.gmail.com
3. Get the current URL and check whether it is containing www.google.com or not

getCSSValue()
Write a script to print the colour of forgotten password link present in Facebook

Assignment

Write a script to print font type & font size of forgotten password link present in Facebook
X-Path axes
 X-Path axes are the special keywords of the x-path which has following syntax;
Syntax: /Axes_Name::tag_name
 Some of the axes name used are:
1. ancestor
2. descendant
3. parent
4. child
5. following-sibling
6. preceding-sibling
Ex: /descendant::a -> //a

tr

1. td 1 java
Preceding-Sibling
2. td 2
3. td
4. td 1 Following-Sibling
selenium
5. td 2
1. a ide
2. a webdriver

Ex:

 //td[3]/preceding-sibling::td[2] -> java


 //td[3]/following-sibling::td[2] -> Selenium
 //td[3]/parent::tr
 //td[5]/child::a[2] -> Web driver

Ex:

HTML Tree
HTML

body
Java 100
table
Selenium 300
tbody

1. tr
1. td Java
2. td 100
2. tr
1. td Selenium
2. td 300

X-Path by axes: //td[text()=’selenium’]/following-sibling::td[2]


 Write a x-path by axes to find the price of 1st roadster t-shirt present in www.myntra.com
X-path: (//h3[text()=’Roadster’])[1]/parent::div/child::div/child::span[1]/child::span[1]

or

(//h3[text()=’Roadster’])[1]/parent::div//descendant::span[@class=’product-discountedPrice’]

Assignment

 Write a x-path by axes to find the price of iPhoneXR present in www.amazon.in


X-path: (//span[contains(text(),'Apple iPhone
XR')])[5]/ancestor::div[2]/child::div[3]/child::div/child::div[1]/child::div[1]/child::div[1]/child::a

X-Path by starts with function


We can use starts with function on the x-path & the syntax is;

Syntax:

//tag_name[starts-with(text(),’value’)]

//tag_name[starts-with(@Attribute_Name,’Attribute_Value’)]

Ex:

<a..>Inbox(7)<a>

X-Path: //a[starts-with(text(),’Inbox’)]
By giving only “box” will not work here

Difference between findElement & findElements


findElement findElements
Return type of findElement is Web Element Return type is List of Web Element
If the locator is not matching with any of the Here it returns empty list
element, it will throw NoSuchElement
Exception
If the locators are matching with multiple It returns all the matching elements
elements, it will return the 1st matching
element
Used to handle single element Used to handle multiple elements

Assignment

 Automate the following scenario:


1. Open the chrome Browser got to www.flipkart.com then search for iPhone 13 Pro Max
capture all the auto suggestions and print the count of it
2. Print all the auto suggestions on the console
3. Select the last auto suggestion
{Hint: //div[@class=’IrtEPN_17d0yO’]}

 Write a script to select all the checkbox for any web application (URL should be taken from the
user) [Hint: //input[@type=’checkbox’]
 Write a script to print all the content of the web table & take URL from the user. [Hint: use
//td]

Synchronization
 The process of matching the selenium speed with the application is called as synchronization
 Most of the time selenium is faster than the application, because of this reason we may not
expected result or we may get exceptions such as NoSuchElement Exception.

Implicit Wait
In selenium there are different ways to synchronize the script, on the of the frequently used option
used is implicit wait.

Syntax: driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

 It will take two arguments;


1. long(Time Duration)
2. Time Units
 Time Units can be days, hours, minutes, seconds, milli seconds, micro seconds, nano
seconds, etc.
 The specified duration is used only by findElement & findElements statement.
 It means implicit wait will work for findElement or findElements only, not for any other
functions

Flow Diagram

 When the control comes to any findElement or findElements statement, it will check
whether any the element is present or not
 If the element is present the findElement returns 1st element matching element, whereas
findElements returns all the matching elements.
 If the specified element is not present then it will check for the time out
 If the time is over findElement will throw NoSuchElement Exception, whereas findElements
returns empty list
 If the time is not over it will wait for 500ms which is called as Pooling Period.
 Then it will continue to check whether element is present or not

Ex:

 Can we specify implicit wait statement multiple times in selenium script ?

Yes.

 Is it necessary to write implicitly wait statement multiple times ?


No

Explicit Wait
 In order to handle the synchronization of any method we can use explicit wait.
 WebDriver wait itself is called as explicit wait, because we have to specify the condition
explicitly
Syntax:
WebDriver driver=new WebDriver(driver,10);
wait.until(ExpectedConditions.titleIs(“Enter the title”);

Flow Diagram
 When the control comes to wait.until statement it will check the specific condition
 If the condition is true it will go to the next statement, if the condition is false it will check
for the time out
 If time out is over, it will TimeOut Exception, else it will wait for 500ms (5s) and it will
continuous to check the condition

NOTE

 In the above flow diagram titleIs is the condition


 By using explicit wait we can handle the synchronization of any statement but only one at a
time.

Write a script to print the title of the homepage?

NOTE

Expected Condition is interface whereas Expected Conditions is a class

 Can we handle the synchronization of the findElement method using explicit wait

Yes

Difference between Implicitly Wait & Explicitly Wait


Implicit Wait Explicit Wait
We do not specify waiting condition explicitly We should specify the waiting condition
explicitly
We can handle the synchronization of all We can handle the synchronization of any
findElement & findElements method, but only one at a time
After the duration we get NoSuchElement After the duration we get TimeOut Exception
Exception
Time durations can be days, hours, mins, sec, Time duration will be only Seconds
etc

TimeOut Exception

You might also like