Selenium Elements
Types of Selenium Locators
Selenium Webdriver Browser Commands
Syntax
Selenium Webdriver Browser Commands
1. get(String arg0): void - This method Load a new web page
in the current browser window
driver.get("https://www.google.com");
2. getTitle(): String - This method fetches the Title of the
current page.
driver.getTitle();
3. getCurrentUrl(): String - This method fetches the string
representing the Current URL which is opened in
the browser
driver.getCurrentUrl();
4. getPageSource(): String - This method returns the Source
Code of the page.
Selenium Webdriver Browser Commands
5. close(): void - This method Close only the current window
the WebDriver is currently controlling
6. quit(): void - This method Closes all windows opened by
the WebDriver.
Practice
1. Launch a new Chrome browser.
2. Open Shop.DemoQA.com
3. Get Page Title name and Title length
4. Print Page Title and Title length on the Eclipse Console.
5. Get Page URL and verify if it is a correct page opened
6. Get Page Source (HTML Source code) and Page Source length
7. Print Page Length on Eclipse Console.
8. Close the Browser.
Answer
import org.openqa.selenium.WebDriver; import
org.openqa.selenium.chrome.ChromeDriver;
public class WebDriverCommands {
public static void main(String[] args) {
String driverExecutablePath = "D:\\Drivers\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverExecutablePath);
WebDriver driver = new ChromeDriver();
String url = "https://www.shop.demoqa.com";
driver.get(url);
String title = driver.getTitle();
int titleLength = driver.getTitle().length();
System.out.println("Title of the page is : " + title);
System.out.println("Length of the title is : "+ titleLength);
String actualUrl = driver.getCurrentUrl();
if (actualUrl.equals(url)) {
System.out.println("Verification Successful - The correct Url is opened."); }
else { System.out.println("Verification Failed - An incorrect Url is opened.");
System.out.println("Actual URL is : " + actualUrl);
System.out.println("Expected URL is : " + url); }
String pageSource = driver.getPageSource();
int pageSourceLength = pageSource.length();
System.out.println("Total length of the Pgae Source is : " + pageSourceLength);
driver.close();
}
Selenium Navigation Commands
Selenium Navigation Commands
1. to(String arg0) : void - This method Loads a new web page
in the current browser window
driver.navigate().to("https://www.DemoQA.com");
2. forward() : void - This method does the same operation
as clicking on the Forward Button of any browser.
driver.navigate().forward();
3. back() : void - This method does the same operation
as clicking on the Back Button of any browser
4. refresh() : void - This method Refresh the current page.
WebElement Commands
WebElement Commands
1. clear( ) : void - If this element is a text entry element, this
will clear the value
WebElement element = driver.findElement(By.id("UserName"));
element.clear();
2. sendKeys(CharSequence... keysToSend ) : void - This
simulates typing into an element, which may set its value.
element.sendKeys("ToolsQA");
3. click( ) : void - This simulates the clicking of any element.
4. isDisplayed( ) : boolean - This method determines if an
element is currently being displayed or not.
5. isEnabled( ) : boolean- This determines if the element
currently is Enabled or not?
WebElement Commands
6. isSelected( ) : boolean - Determine whether or not this
element is selected or not.
7. submit( ) : void- This method works well/better than the
click() if the current element is a form, or an element within a
form.
8. getText( ) : String- This method will fetch the visible (i.e. not
hidden by CSS) innerText of the element.
9. getTagName( ) : String- This method gets the tag name of
this element.
10. getCssvalue( ) : String- This method Fetch CSS property
value of the give element.
WebElement Commands
11. getAttribute(String Name) : String- This method gets the
value of the given attribute of the element.
String attValue = element.getAttribute("id");
12. getSize( ) : Dimension - This method fetch the width and
height of the rendered element.
Dimension dimensions = element.getSize();
System.out.println(“Height :” + dimensions.height + ”Width : "+
dimensions.width);
13. getLocation( ) : Point - This method locate the location of
the element on the page.
Point point = element.getLocation(); System.out.println("X
cordinate : " + point.x + "Y cordinate: " + point.y);
Relative Locators
Relative locators are used to identify an element which
does not have enough information to locate it uniquely
on a web application. For detecting it, we can identify it
spatially with respect to another element
import
org.openqa.selenium.support.locators.RelativeLoc
ator
Relative Locators used in Selenium Webdriver
• above()
• below()
• near()
• toLeftOf()
• toRightOf()
Example - above() Relative Locator
We will locate Practice form above login form
Syntax
WebDriver driver = new ChromeDriver();
// identify element the first element
WebElement l = driver.findElement(By.xpath("value of xpath locator"));
// identify element above the first link element
WebElement e =
driver.findElement(RelativeLocator.with(By.tagName("a")).above(l));
Code
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.locators.RelativeLocator;
import java.util.concurrent.TimeUnit;
public class RelativeLocatorsAbove {
public static void main(String[] args) throws InterruptedException
{ // Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// launching a browser and navigate to a URL
driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_a
utomation_practice.php");
Code
// identify first element WebElement l =
driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[2]/a")); //
identify element above the first element
WebElement e =
driver.findElement(RelativeLocator.with(By.tagName("a")).above(l));
// Getting element text value the above identified element
System.out.println("Getting element text: " + e.getText());
// Closing browser
driver.quit();
}
}