Selenium Qspiders on eclipse with JAVA
- The WebDriver interface is child of SerchContext interface. It has 13 methods as follows:-
- The JavaScriptExecutor interface contains 2 methods as follows:-
- The TakesScreenshot interface has 1 method:-
- It is used to enter url
- It also waits till first page is completly loaded
- Syntax:-
driver.get("url");
- It is used to fetch title of page
- Syntax:-
String title = driver.getTitle(); //getTitle()
System.out.println("\nTitle of Page is:- "+title);
- It is used to fetch url of current page
- Syntax:-
String url = driver.getCurrentUrl(); //getCurrentUrl()
System.out.println("\nurl of Page is:-"+ url);
- It is used to fetch html code of current page
- Syntax:-
String page = driver.getPageSource(); //getPageSource()
System.out.println("\nSource code of Page is:- "+page);
- This method is used to maximize or minimize window
- Syntax:-
driver.manage().window().maximize(); //manage()
driver.manage().window().minimize();
- The close() method is a Webdriver command that closes the browser window currently in focus.
- Syntax:-
driver.close();
- The quit() command quits the entire browser session with all its tabs and windows..
- Syntax:-
driver.quit();
- switchTo() method is used to type text in active element.
- Any element where cursor is blinking is called Active element.
- switchTo() is also used to switch between frames.
- Syntax:-
String myWindowHandle = driver.getWindowHandle();
//Redirect to the page you want and then call switchTo()
driver.switchTo().window(myWindowHandle );
-
Navigation commands are those commands in Selenium that are used to perform different operations such as backward, forward, refresh, wait, etc in the browser’s history.
-
It has 3 types
- Navigate forward: This method is used to move forward by one page on the browser’s history.
- Syntax:
driver.navigate().forward();
- Navigate Back : This method is used to move back by one page on the browser’s history.
- Syntax:
driver.navigate().back();
- To Command: This method is used to navigate the URL of the web page in the current browser window. It will just navigate to the web page but not wait till the whole page gets loaded.
- Syntax:
driver.navigate().to(URL);
- To refresh: This method is used to refresh the current web page.
- Syntax:
driver.navigate().refresh();
- Navigate forward: This method is used to move forward by one page on the browser’s history.
- get.windowhandle(): helps in getting the window handle of the current window
- Syntax
String ParentWindowHandle = driver.getWindowHandle();
- get.windowhandles(): helps in getting the handles of all the windows opened
- set: helps to set the window handles which is in the form of a string.
set<string> set= driver.get.windowhandles()
- A command used to uniquely identify a web element within the web page.
- Returns the first matching web element if multiple web elements are discovered by the locator
- Throws NoSuchElementException if the element is not found
- Detects a unique web element
- Syntax for findElements()
WebElement elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));
- A command used to identify a list of web elements within the web page.
- Returns a list of multiple matching web elements
- Returns an empty list if no matching element is found
- Returns a collection of matching elements
- findElement() and findElements() have 8 locators
- Syntax for findElements()
List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));
- Example
List<WebElement> listOfElements = driver.findElements(By.xpath("//div"));
- ID is uniquely defined for each element
- driver will locate an element by "id" attribute
- Syntax:-
driver.findElement(By.id(" "));
- Example
WebElement loginTB = driver.findElement(By.id("email"));
- Where
email
is 'id'.
- driver will locate an element by "name" attribute.
- Syntax:-
driver.findElement(By.name(" "));
- Example
driver.findElement(By.name("login"));
- Where
login
is 'name'.
- Syntax:-
driver.findElement(By.className(" "));
- Example
driver.findElement(By.className("inputtext _55r1 _6luy"));
- Where
inputtext _55r1 _6luy
is 'ClassName'.
- Syntax:-
driver.findElement(By.tagName(" "));
- Example
WebElement loginBtn = driver.findElement(By.name("login"));
- Where
login
is 'name'.
- LinkText is helpful to find links in a webpage. It is the most efficient way of finding web elements containing links.
- Syntax:-
driver.findElement(By.linkText(" "));
- Example
WebElement loginBtn = driver.findElement(By.name("login"));
- Syntax:-
driver.findElement(By.partialLinkText(" "));
- Example
WebElement loginBtn = driver.findElement(By.name("login"));
- CSS Selectors are one of the locator strategies offered by Selenium to identify the web elements.
- The CSS Selectors mainly use the character sequence pattern, which identifies the web elements based on their HTML structure.
- Syntax:-
tag[Attribute_Name = 'Attribute_value']
- CSS Selectors can be created based on the following ways:
- Syntax
tag[id = 'id_value']
- Example:-
//For password TextField in facebook login page
driver.findElement(By.cssSelector("input[id='pass']"));
//OR
driver.findElement(By.cssSelector("input#pass"));
- Where
pass
is 'id'. - The
'#'
sign symbolizes the 'ID' in CSS Selector.
- Syntax
tag[class = 'class_value']
- Example:-
//For password TextField in facebook login page
driver.findElement(By.cssSelector("input[class='inputtext _55r1 _6luy _9npi']"));
//OR
driver.findElement(By.cssSelector("input.inputtext _55r1 _6luy _9npi"));
- Where
inputtext _55r1 _6luy _9npi
is 'className'. - The
.
dot sign symbolizes the 'class' in CSS Selector.
- Apart from the id and class attributes, all other attributes present within the HTML tag of the element can also be used to locate web elements using the CSS Selectors.
- Syntax:-
tag[Attribute_Name = 'Attribute_value']
- Example
WebElement loginTB = driver.findElement(By.cssSelector("input[type='text']"));
loginTB.sendKeys("bamlutuyde@vusra.com");
- Where
input
istag
. type
isAttribute_Name
.text
isAttribute_value
.
-
we combine multiple attributes to precisely locate any element present on the web page.
-
Combine the ID and other Attributes
- Syntax:-
tag#ID_Value[Other_AttributeName='Other_AttributeValue']
- Example
input#pass[placeholder='Password']
- where
input
is 'tagName'pass
is 'ID_Value'placeholder
is 'Other Attribute Name'Password
is 'Other Attribute Value'
driver.findElement(By.cssSelector("input#pass[placeholder='Password']"));
- Syntax:-
-
Combine the Class and other Attributes
- Syntax:-
tag.Class_Value[Other_AttributeName='Other_AttributeValue']
- Example
input.inputtext _55r1 _6luy _9npi[placeholder='Password']
- where
input
is 'tagName'inputtext _55r1 _6luy _9npi
is 'class_Value'placeholder
is 'Other Attribute Name'Password
is 'Other Attribute Value'
driver.findElement(By.cssSelector("input.inputtext _55r1 _6luy _9npi[placeholder='Password']"));
- Syntax:-
-
Xpath are of 2 types:-
- In AbsoluteXpath syntax starts from
.
- We use
.
for current html program / directory. - We use
/
for child element. - We use
/..
for parent element. - We use
//
for desendnt element(child, grand-child, great-grand-child). - We use
[]
to specify index value.In html index value starts from 1. - eg.
WebElement a1 = driver.findElement(By.xpath("./html/body/div[1]/a[1]"));
a1.click();
- Relative Xpath starts with
//
. - We use
//
for desendnt element(child, grand-child, great f=grand-child). - eg.
//For link 1 GOOGLE
driver.findElement(By.xpath("//div[1]/a[1]")).click();
- link.html file
Element | Absolute Xpath | Relative Xpath |
---|---|---|
./html/body/div[1]/a[1] |
//div[1]/a[1] |
|
T3 | ./html/body/div[2]/input[2] |
//input[2] |
Google , FB | ./html/body/div[1]/a |
//div[1]/a |
Google ,Amazon | ./html/body/div/a[1] |
//a[1] |
T1 ,T2 ,T3 | ./html/body/div/input |
//input |
T1 ,T2 | ./html/body/div/input[1] |
//input[1] |
Google ,FB ,Amazon | ./html/body/div/a |
//a |
There are several types of Relative Xpath
- Xpath_by_Attribute
- Xpath_by_TextFunction
- Xpath_by_ContainsFunction
- Xpath_by_Dependant_Independant
- Xpath_by_Group_Index
- Xpath_by_AxesTag
- XPath expression select nodes or list of nodes on the basis of attributes like ID , Name, Classname, etc.
- Syntax:-
//tag[@AttributeName='AttributeValue']
- Example:-
//To find Login TextBox in Facebook login page.
driver.findElement(By.xpath("//input[@type='text']")).sendKeys("bamlutuyde@vusra.com");
- Syntax:-
//tag[.='text Value']
**OR**
//tag[text()='text Value']
**OR**
//*[text()='text Value']
- Example:-
//To find password TextBox in Facebook login page.
driver.findElement(By.xpath("//span[.='Log Out']")).click();
//OR
driver.findElement(By.xpath("//span[text()='Log Out']")).click();
//OR
driver.findElement(By.xpath("//*[text()='Log Out']")).click();
-
The contain feature has an ability to find the element with partial text.
-
Contains Method has 2 types:-
- Syntax:-
//tag[contains(@AttributeName,'Partial Attribute Value')]
- Example:-
driver.findElement(By.xpath("//a[contains(@href,'google')]")).click();
- Syntax:-
//tag[contains(text(),'Partial Text Value')] **OR** //tag[contains(.,'Partial Text Value')]
- Example:-
driver.findElement(By.xpath("//a[contains(text(),'Facebook')]")).click(); driver.findElement(By.xpath("//a[contains(.,'Amazon')]")).click();
- It is also called as Xpath Traversing.
- In this type of Xpath there are elements which changes with respect to time.
- Steps:-
- Write xpath for independant element.
- Trverse towards dependant element.
- In AbsoluteXpath syntax starts from
.
- We use
.
for current html program / directory. - We use
/
for child element. - We use
/..
for parent element. - We use
//
for desendnt element(child, grand-child, great-grand-child). - We use
[]
to specify index value.In html index value starts from 1. - Example
driver.findElement(By.xpath("//a[.='JAVA']/../../td[2]/a")).click();
- In this type of xpath, we specify the xpath within common brackets
()
. - By doing this an xpath array is generated starting with index
1
. - Unique indexes are given for every matching element.
- At end of xpath we specify the index value within Square Brackets
[]
. - Syntax:-
(//tagName)[IndexValue];
- Example:-
driver.findElement(By.xpath("(//a)[1]")).click(); //For Link 1 a[1] Google
-
Different XPath Axes Used In Selenium Testing:-
-
child:- This indicates the children of the context node
- Syntax:-
/child::tagName
-
parent:- This indicates the parent of the context node
- Syntax:-
/parent::tagName
-
descendent:- This indicates the children, grandchildren, and their children (if any) of the context node.
- Syntax:-
/descendant::tagName
-
following-sibling:- This one indicates all the sibling nodes (same parent as the context node) that appear after the context node .
- Syntax:-
/following-sibling::tagName
-
preceding-sibling:- This one indicates all the sibling nodes (same parent as context node) that appear before the context node
- Syntax:-
/preceding-sibling::tagName
-
-
Example for Axes Tag
driver.findElement(By.xpath("//a[.='JAVA']/parent::td/following-sibling::td/child::a")).click();