0% found this document useful (0 votes)
283 views

Element Handling in Selenium Part 3

The document discusses various ways of handling common web elements like links, checkboxes, buttons, radio buttons, images, dropdowns in Selenium WebDriver. It provides the manual actions that can be performed on each element and the corresponding Selenium code to do the same actions like clicking links, selecting checkboxes, validating element properties etc. Steps are given to retrieve link text, check link status, click links. Similarly for other elements like handling checkboxes, buttons, radio buttons, images, dropdowns.

Uploaded by

Steve Smith
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)
283 views

Element Handling in Selenium Part 3

The document discusses various ways of handling common web elements like links, checkboxes, buttons, radio buttons, images, dropdowns in Selenium WebDriver. It provides the manual actions that can be performed on each element and the corresponding Selenium code to do the same actions like clicking links, selecting checkboxes, validating element properties etc. Steps are given to retrieve link text, check link status, click links. Similarly for other elements like handling checkboxes, buttons, radio buttons, images, dropdowns.

Uploaded by

Steve Smith
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/ 9

G C Reddy Technologies (www.gcreddy.

com)

Selenium Class 28 - Element


Handling in Selenium Part-3

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...
.....................................
Manual Actions on Link Element

- Check Displayed Status (Visible/Displayed/Exists)


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

Selenium WebDriver Steps:

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

G C Reddy Technologies (www.gcreddy.com)


G C Reddy Technologies (www.gcreddy.com)

WebDriver driver = new ChromeDriver();


driver.manage().window().maximize();

driver.get("https://www.google.com/");
//Check Link Existence
boolean linkPresent= driver.findElement(By.linkText("Gmail")).isDisplayed();
System.out.println(linkPresent);

//Check Enabled status


boolean linkStatus = driver.findElement(By.linkText("Gmail")).isEnabled();
System.out.println(linkStatus);

//Return Link Name


String linkName = driver.findElement(By.linkText("Gmail")).getText();
System.out.println(linkName);

//Click(Redirects)Link
driver.findElement(By.linkText("Gmail")).click();

Thread.sleep(2000);
driver.close();
}
}

G C Reddy Technologies (www.gcreddy.com)


G C Reddy Technologies (www.gcreddy.com)

Using 2nd Syntax:

driver.get("https://www.google.com/");
WebElement gmailLink = driver.findElement(By.linkText("Gmail"));

System.out.println(gmailLink.isDisplayed());

System.out.println(gmailLink.isEnabled());

System.out.println(gmailLink.getText());

gmailLink.click();

Thread.sleep(4000);

driver.close();

5) Handling Checkbox

Manual Actions:

- Check Displayed Status


- Check Enabled Status
- Check Selected status

- Select (Click)
- Unselect (Click)

G C Reddy Technologies (www.gcreddy.com)


G C Reddy Technologies (www.gcreddy.com)

Selenium WebDriver Steps:

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

WebElement checkBox = driver.findElement(By.name("newsletter"));

//Check the Element Displayed Status


boolean status = checkBox.isDisplayed();
System.out.println(status);//true

//Check the Element Enabled Status


status = checkBox.isEnabled();
System.out.println(status);//true

//Check the Element Selected Status


status = checkBox.isSelected();
System.out.println(status);//false

//Select the Checkbox


checkBox.click();

//Check the Element Selected Status


status = checkBox.isSelected();
System.out.println(status);//true

G C Reddy Technologies (www.gcreddy.com)


G C Reddy Technologies (www.gcreddy.com)

//Select the Checkbox


checkBox.click();

//Check the Element Selected Status


status = checkBox.isSelected();
System.out.println(status);//false

driver.close();

6) Handling Button

Manual Actions:

- Check Displayed Status


- Check Enabled Status

- Click

Selenium WebDriver steps:

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

boolean displayStatus =
driver.findElement(By.xpath("//*[@id='identifierNext']/content/span")).isDis
played();
System.out.println(displayStatus);

G C Reddy Technologies (www.gcreddy.com)


G C Reddy Technologies (www.gcreddy.com)

boolean enabledStatus =
driver.findElement(By.xpath("//*[@id='identifierNext']/content/span")).isEn
abled();
System.out.println(enabledStatus);

driver.findElement(By.xpath("//*[@id='identifierNext']/content/span")).click
();

7) Radio Button

Manual Actions:

- Check Displayed Status


- Check Enabled Status
- Check selected Status

- Select (Click)

Selenium WebDriver Steps:

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

//Check Display Status


boolean displayStus = driver.findElement(By.name("gender")).isDisplayed();

//Check Enabled Status


boolean enbledStatus = driver.findElement(By.name("gender")).isEnabled();

G C Reddy Technologies (www.gcreddy.com)


G C Reddy Technologies (www.gcreddy.com)

//Check Selected Status


boolean selectedStatus =
driver.findElement(By.name("gender")).isSelected();

System.out.println(displayStus);//true
System.out.println(enbledStatus);//true
System.out.println(selectedStatus);//false

//Select the Radio Button


driver.findElement(By.name("gender")).click();

//Check Selected Status


selectedStatus = driver.findElement(By.name("gender")).isSelected();//true
System.out.println(selectedStatus);

8) Handle Image, Image Button and Image Link

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

//Return Google Image name


String imageName = driver.findElement(By.id("hplogo")).getAttribute("alt");
System.out.println(imageName);

driver.navigate().to("http://newtours.demoaut.com/");
//Click Image Button
driver.findElement(By.name("login")).click();
Thread.sleep(3000);

G C Reddy Technologies (www.gcreddy.com)


G C Reddy Technologies (www.gcreddy.com)

driver.navigate().to("https://www.seleniumhq.org/");
Thread.sleep(3000);
//Click Image Link
driver.findElement(By.xpath("//*[@id='choice']/tbody/tr/td[2]/center/a/img
")).click();
}
}

9) Handle Drop down Box

Manual Actions:

- Check Displayed Status


- Check Enabled Status

- Select an Item
- Return Items Count

Selenium WebDriver Steps:

driver.get("http://www.gcrit.com/build3/create_account.php?osCsid=6rqlkh
hibc3d4pvkdt6fafs4s1");

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

G C Reddy Technologies (www.gcreddy.com)


G C Reddy Technologies (www.gcreddy.com)

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

Select dropdown = new Select (driver.findElement(By.name("country")));


//dropdown.selectByIndex(6);

dropdown.selectByVisibleText("India");
Thread.sleep(4000);

List <WebElement> e = dropdown.getOptions();


System.out.println(e.size());
driver.close();

G C Reddy Technologies (www.gcreddy.com)

You might also like