Selenium Basics Notes

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

URL LINK https://leaftaps.

com/opentaps

selenium interacts thorugh back end

DOM-->document object Model


1. its an page contain the source code of the web page, whcih will be in HTML
format
2.anything comes between the <> is an html tag , <html>...<html>
you will be able to write the HTML code
3.<head>...<head>- used to configure the header and title of the sire
4.<boby>..<body> - used to design the entire web page
5.<div>....</div> & <span>....</span> - used to create a partiton of the page
6.<input> - used to create text box, button
7.<button>- used to create a button
8. <a> - used t crete a link
9.concept of finding the particular element to interact with -> locator
10.<input class="inputLogin" type="text" id="username" name="USERNAME" size="50">
11. different elements and tages for above
input --> tag name
class,type,id,name,size --. attribute

{for class - "inputlogin" is the value


fo rtype - " text"} is the value

<label for = "password">password</label>


tag name -> label
attribute -> for
value - > password
text --> password

12. how to find the locators

LOcators IDE web driver when do you use

ID id id Always(most preferred)

Name name name IS "id" doesnot exist

link link linkText is it is a link

Tagname X tagname for collection of objects

CLass name X className whn a class is unique w/o

XPath xpath xpath if none of the above works

Style sheet css cssselector this is the last option you have!

13. when you find the source code of particular element , inthat
we find the attribute as id, we can you use it as an locator

14. LInk and partial text

when you have id , class and name attributes in the soiurce code of the element
that you want to interact .
then you can use them as locators as bove in 13 step
15. when you find a <a> -> link -> you should use linktext a a locator
16. partial link text as a locator
17. when not to use
id = "username-123", will not use id when it has number in it
class = "leaftaps login username " space is not allowed
name = duplicate not allowed
linktext and partial linktext - <a> whn its not a tag we should not use it
18. to find the webelement , return type short cut is crt2 L
19. link text is an case sensitive so just copy from the source code
20. To handle drop down developed in <select> has select class

dropdown will be created in select tag and under select tag will have option
tag , which will be useful in creting the options in the select tag
21. IN sleenium , to handle the drop down develpoed in the <select> tag has selelct
class ,
this class i sspecially made for drop down
with condition as should have developed in select class(select is an class)

// way to handle drop down


// elementSourceDD.sendkeys("EMployee");

//Find the drop down element in sleect tag


WebElement elementSourceDD = driver.findElement(by.id("Crete...."));

Select dd = new select(element)


//1 . using index we can select the source
dd.selectByIndex(index);
//eg in drop down (1.a. 2.B...) in that
if i selct number 4 the value in the index 4 will be displayed
//2.Select by using visible text
dd.selectByvisibletext("");

in option tag, you may have an attribut called value in the option tag , using
"..." we can use
dd.selectByValue("....");

WebELement elemetindustryDD = Driver.findELement(By.id("...."));


Select dd1 = new Select(elemetindustryDD);
for every drop down object should be mentioned (elemntindustryDD whc=ich is also
mentioned in the second
bracket as well , )in the constructor
===
Selenium basics session- 2

XPATH

LOcators Id, CLass, Name


LinkText, Partial LinkText
Tagname
Xpath &CSS

Preferences of selecting the locators,


1.# 1ID #1 LinkText , #1 partial LinkText[<a> - prefer only linktext]
2.#2 class. #2 Name
3.Xpath
4.CSS, tagname

Why xpath? there is an pdf for xpath in the documents folder

Xpath in an XML path -is an route to the element


different types of Xpath
Absoulte X path
relative x path

Difference between absoulte and relative x path

Absoulte xpath - it should start from / html tag - will give you path from HTML
tag to the element i am searching for(exact x path )
relative xpath - it should start from // - based on the element i am
searching for(it will derive to the particular element)

letting to understand the address route


AB xpath =step by step
relative = near by location and asking to come casa grande

ususally we will use relative xpath than absoulte xpath

Relative xpath, types

1. Basics
1.1 attribute based xpath : other attributes as a locators
for example : <label for="username">Username</label>
<input class="inputLogin" type="text" id="username" name="USERNAME"
size="50">
attribute for can be used
when you have css attributes should not be used like size, font all these are
size based locators

Attribute FOR can be used


syntax: //tagname[@attribute = 'value']
xpath: // label[@for= 'username']

Partial
syntax: //tagname[contains@attribute, 'value']
xpath: // form[[contains@action,'']
driver.findElemet(By.xpath("label[@for= 'username']"));
{to check if the xpath is correct just paste it in the find box and check }

1.2 text based xpath : text as locator


Syntax : //tagname[text() = 'value']
Xpath ; //Label[text() = 'Username']

Partial :
Syntax : //tagname[containstext() ,'value']
Xpath ; //Label[contains(text(),'erna']

1.3 collection - finite duplicate


Synatx : (//xpath)[index]
xpath : (//xpath)[1]
for Xpath index will start with 1
(//input[@class = 'input'])[2]

2.Axes - Based on relationship


1. parent to child
SYntax :parent tag Xpath/child tag name or xpath
xpath : //p[@class='top']/label or //p[@class='top']/label[@for='username']
2. child to parent
SYntax :child tag Xpath/parent :: parent tag name or xpath
xpath : //label[text()= 'username']/parent::p

3. grand parent to grand child


Syntax : grand parent xpath // grand child tag name or xpath
xpath : //form[@id = 'login']//label
if you find the duplicate index needs to be used
(//form[@id = 'login']//label)[1]
4.grand child to grand parent
Syntax : grand parent xpath /ancestor :: grand parent tag name or xpath
xpath : //label[text()='usrname']/ancestor::form

5. elder sibling to younger sibling


in order to find the sibling ... /following-sibling::input
SYntax : elder sibling xpath/following-sibling::younger sibling xpath or tagname
Xpath ://label[text()= 'Username']/following-sibling::input

6. younger silbling to elder sibling


SYntax : younger sibling xpath/ preceding-sibling::elder sibling xpath or tagname
Xpath ://input[@id='username']/preceding -sibling::label

7. elder cousin to younger cousin


syntax : Elder cousin xpath/following::younger cosuin xpath or tagname
xpath : //label[text()='username']/following-cousin

8.younger cousin to elder cousin


syntax : younger cousin xpath/ preceding :: elder cosuin xpath or tagname
xpath : //label[text()='Password']/preceding::label

WEB TABLE:

<table> inside table tag will have <tbody>


<tr></tr> tr means table row
inside this tr will have <td is table data
</table>

Session 3

Selenium - ADV
snapshot

in target LOcators
ALert
frame
window

SNAPSHOT

How to take the snap shot

1.Printscrn -> Press I will take a snap shot


2.MSPaint-> paste the content in my clipboard
3.To open the clipboard there an short cut
4.save the image
5.Prntscrn
driver.manage().timeouts().implicitlyWait(Duration.ofSecons(30),

//Take ScreenSHot
File Source = driver.getScreenshotAs(OutoutType.FILE);
//Creating an empty image to store my ss
File dest = new File(./snaps/pic001.jpg);
//Merge the ss with image
FileUtils.copyfile(source,dest);

ALert
Example :

Fire alert
Rain ALert
Alarm
Amma wakeup
salary credited
battery low

Basic of alert
Alert is model pop up
Alert is a part js

1.when the alert is on, entire DOM will be frozen


2.

Members of alerts
Alert text
Text field
button - yes /no

Types of alerts
simplet Alert - only text and one button
confirm alert -

<Frame>
House-> Bedroom-> Bathroom
Bathroom-> bedroom-> house

SESSION $
https://jqueryui.com/draggable/
https://jqueryui.com/droppable/
https://jqueryui.com/selectable/

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.1.5</version>
</dependency>

Relative xpath
1. Basics
1.1 attribute based xpath: other attributes as a locator - class with space
Equals:
Syntax: //tagname[@attribute = 'value']
xpath: //label[@for = 'username']

Partial:
Syntax: //tagname[contains(@attribute , 'value')]
xpath: //form[contains(@action , '/opentaps')]

1.2 text based xpath: text as a locator


Equals:
Syntax: //tagname[text() = 'value']
xpath: //label[text() = 'Username']

Partial:
Syntax: //tagname[contains(text() , 'value')]
xpath: //label[contains(text() , 'erna')]

1.3 collection - finite duplicate - index starts with 1


Syntax: (//xpath)[index]
xpath: (//input[@class='inputLogin'])[2]

You might also like