Professional Documents
Culture Documents
Selenium Notes
Selenium Notes
A Java interface defines a set of methods but does not implement them. A class that
implements the interface agrees to implement all of the methods defined in the
interface.
The interface in java is a mechanism to achieve fully abstraction. There can be only
abstract methods in the java interface not method body. It is used to achieve fully
abstraction and multiple inheritance in Java.
"A method which is incomplete and to be completed in its child class is called an
abstract method".
Let's take a real world scenario. You have declared a class named Vehicle which
contains a method drive(). drive () defines the mechanism that how a particular
vehicle can be driven. Since there may be different driving mechanism for different
vehicles, we will not write the method definition for drive(). ie, we would declare
that method as abstract.
Now, let's assume that we have two classes Bike and Car and these both are
extending Vehicle. So, we can define our drive() method in Bike and Car classes as
per our requirements.
Set Property :
All the browsers given exe files to invoke the browser from selenium. So we need
assign it before the webdriver launch
System.setproperty (“webdriver.chrome.driver”,
"D:\Selenium\chromedriver.exe")
It is (key, value) syntax where Key is webdriver.chrome.driver and value is
D:\Selenium\chromedriver.exe for chrome
For Firefox, Key is webdriver.gecko.driver and for IE, webdriver.ie.driver
Locators – 8
ID
Name
Linktext
Partial Linktext
Tag Name
Class Name
CSS Selector
Xpath
ID :
Driver.findelement(By.id(“”))
If the id contains alphanumeric, it is dynamic and we shouldn’t use that
Name
Driver.findelement(By.name(“”))
Linktext
Driver.findelement(By.linkText(“”))
Confirm the link object with anchor "a" tag
Class Name
Driver.findelement(By.classname(“”))
Classes should not have spaces- Compound classes cannot be accepted
Xpath
Right click on the DOM, click on copy , take the xpath, using xpath option
Driver.findelement(By.Xpath(“//*[@attribute = ‘value’]”))
CSS
Right click on the DOM, click on copy , take the css using selector option
Driver.findelement(By.cssSelector(“//[attribute = ‘value’]”))
tagName#id
When we don’t have unique or static attributes, we have to write an xpath with
the help of parent or child xpath only
Identify the parent xpath and traverse the child using tagName
Siblings
U cannot travel between the siblings. You have to travel back to parent from child
and have to travel to the respected child
“Xpath/following-sibling:li[2]”
“Xpath/parent:Ul[2]”
//tagName[text()= ‘Text’]
Static dropdowns
Static dropdowns will always have same values and it will have “Select” tag
Selenium itself having separate class called “Select” for this static dropdown
Create an object for the Select class and pass the respected element of the drop
down as an argument into that class.
After that,
Dynamic dropdowns
This is very simple , find the xpath of the required element and its count in overall
page.
For Example, if we are required to select the Chennai value from [To] in a Airport
sight, Chennai will be presented in both [From] and [To], Where From will the 1st
index and To is the 2nd index. So we need to give the value [2] along with the Xpath
as below
(Xpath) [2]
Interview Question :
How can you select the element from dynamic dropdown without using its index ?
Ans: Using the Parent – child combo
That is, Identify the Parent xpath space child xpath (which is the actual xpath)
For Example,
Chennai is available in both [From] and [To]. If u want to select the Chennai of [To]
dropdown, identify the xpath of [To] give space again identify the Xpath of Chennai.
e.g. Xpath of [To] is
//div[@id='glsctl00_mainContent_ddl_destinationStation1_CTNR'
Xpath of [Chennai] inside the [To] is //a[@value='MAA']"
Final Xpath will be
//div[@id='glsctl00_mainContent_ddl_destinationStation1_CTNR'Space
//a[@value='MAA']"