0% found this document useful (0 votes)
21 views8 pages

Element Handling in Selenium Part 2

The document discusses handling different elements in Selenium such as browsers, edit boxes, text areas, error messages, popups, and links. It provides the Selenium WebDriver code to launch and manipulate browsers, enter and clear text in edit boxes, retrieve text from text areas and error messages, handle popups, and click links. Code examples are given for navigating to URLs, getting page titles and sources, maximizing windows, and selecting elements by name, ID, XPath, and CSS.

Uploaded by

Shahul Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views8 pages

Element Handling in Selenium Part 2

The document discusses handling different elements in Selenium such as browsers, edit boxes, text areas, error messages, popups, and links. It provides the Selenium WebDriver code to launch and manipulate browsers, enter and clear text in edit boxes, retrieve text from text areas and error messages, handle popups, and click links. Code examples are given for navigating to URLs, getting page titles and sources, maximizing windows, and selecting elements by name, ID, XPath, and CSS.

Uploaded by

Shahul Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

G C Reddy Technologies (www.gcreddy.

com)

Selenium Class 27: Element Handling


in Selenium Part-2

1) Handling Browser

Manual Operations:

- Lunch the Browser


- Navigate to a specified url
- Return Browser Title
- Return Browser Url
- Return Page Source
- Return Browser Window Handle
- Closed Focused Browser
- Close all Browser that opened by WebDriver during execution
..............................
- Navigate to another url
- Navigate back to previous url
- Navigate Forward
- Navigate Refresh (Refresh the Browser)
............................
- Maximize Browser Window
- Maximize Browser Window
- Full Screen Browser Window
...............................................................................

G C Reddy Technologies (www.gcreddy.com)


G C Reddy Technologies (www.gcreddy.com)

Selenium WebDriver Steps:

System.setProperty("webdriver.chrome.driver", "E:/chromedriver.exe");

//Create Edge Browser Driver for WebDriver interface


WebDriver driver= new ChromeDriver();//Launches Chrome Browser window
with blank url

driver.manage().window().maximize();//maximize the Browser Window

//Full Screen the Browser window


//driver.manage().window().fullscreen();

//Navigate to a specified url


driver.get("http://www.google.com");

//Navigate to another url


driver.navigate().to("http://in.Yahoo.com");

//Navigate Back to Previous url


driver.navigate().back();

//Navigate Forward
driver.navigate().forward();

//Refresh the Browser window


driver.navigate().refresh();

G C Reddy Technologies (www.gcreddy.com)


G C Reddy Technologies (www.gcreddy.com)

//Return Browser Title


System.out.println(driver.getTitle());

//Return Browser Current URL


System.out.println(driver.getCurrentUrl());

//Return Window handle


System.out.println(driver.getWindowHandle());

//Return Page Source


System.out.println(driver.getPageSource());
}
}
...............................................................................

2) Handling Edit Box

Manual Actions:
> Check for Displayed Status
> Check for Enabled Status
> Enter Some Value
> Return the Value
> Clear the Value
Etc...

Username Edit Box


type - text - getAttribute("type"); - text
value- abcd - getAttribute("value"); - abcd

G C Reddy Technologies (www.gcreddy.com)


G C Reddy Technologies (www.gcreddy.com)

Password Edit Box


type -password - getAttribute("type"); - password
...............................................................................
Selenium WebDriver Steps:

System.setProperty("webdriver.chrome.driver", "E:/chromedriver.exe");

//Create Edge Browser Driver for WebDriver interface


WebDriver driver= new ChromeDriver();//Launches Chrome Browser window
with blank url

driver.manage().window().maximize();//maximize the Browser Window


driver.get("http://www.gcrit.com/build3/admin/");

boolean displayedStatus =
driver.findElement(By.name("username")).isDisplayed();
System.out.println(displayedStatus);//true

boolean enabledStatus =
driver.findElement(By.name("username")).isEnabled();
System.out.println(enabledStatus);//true

driver.findElement(By.name("username")).sendKeys("abcd");

System.out.println(driver.findElement(By.name("username")).getAttribute("t
ype"));

driver.findElement(By.name("username")).clear();

G C Reddy Technologies (www.gcreddy.com)


G C Reddy Technologies (www.gcreddy.com)

driver.close();
...............................................................................
Using 2nd Syntax:

driver.manage().window().maximize();//maximize the Browser Window


driver.get("http://www.gcrit.com/build3/admin/");

WebElement editBox = driver.findElement(By.name("username"));


boolean displayedStatus = editBox.isDisplayed();
System.out.println(displayedStatus);//true

boolean enabledStatus = editBox.isEnabled();


System.out.println(enabledStatus);//true

editBox.sendKeys("abcd");

System.out.println(editBox.getAttribute("type"));

editBox.clear();

driver.close();
...............................................................................

G C Reddy Technologies (www.gcreddy.com)


G C Reddy Technologies (www.gcreddy.com)

3) Handling Text Area, Error Message and Popup Window

Selenium WebDriver Steps:

System.setProperty("webdriver.chrome.driver", "E:/chromedriver.exe");

WebDriver driver= new ChromeDriver();//Launches Chrome Browser window


with blank url

driver.manage().window().maximize();//maximize the Browser Window


driver.get("http://www.gmail.com/");
Thread.sleep(3000);

//Return Text Area


String textArea =
driver.findElement(By.xpath("//*[@id='headingSubtext']/content")).getText(
);
System.out.println(textArea);

driver.findElement(By.cssSelector("#identifierNext > content")).click();


Thread.sleep(2000);

//Return Error Message


String errorMessage =
driver.findElement(By.xpath("//*[@id='view_container']/div/div/div[2]/div/d
iv[1]/div/form/content/section/div/content/div[1]/div/div[2]/div[2]")).getTe
xt();
System.out.println(errorMessage);

G C Reddy Technologies (www.gcreddy.com)


G C Reddy Technologies (www.gcreddy.com)

//Handle Popup Window


driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
driver.findElement(By.name("proceed")).click();

Alert popup = driver.switchTo().alert();//Switch driver focus from the web


page to popup window
String errMessage = popup.getText(); //Captures the error message from
the window popup
System.out.println(errMessage);
Thread.sleep(2000);

popup.accept();
driver.findElement(By.id("login1")).sendKeys("India123");
...............................................................................

4) Handling Link

Types of Links in the Web....


i) User Interface Design Point view
a) Text Link (ex: "Gmail" link in Google Home Page)
b) Image Link (ex: Selenium IDE Link in seleniumhq.org)

ii) Functionality Point view


a) Internal Link (It redirects to another location in the same page or another
page in the same application)
b) Out going Link (It redirects to another application)
c) MailTo
Etc...
.....................................

G C Reddy Technologies (www.gcreddy.com)


G C Reddy Technologies (www.gcreddy.com)

Manual Actions on Link Element

> Check Displayed Status (Visible/Displayed/Exists)


> Check Enabled Status
> Click
-----------------
> Return Element Name etc...
...............................................................................

G C Reddy Technologies (www.gcreddy.com)

You might also like