28 – Handling Elements in Selenium Part-3

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”);
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();
}
}

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

  • Check Displayed Status
  • Check Enabled Status
  • Check Selected status
  • Select (Click)
  • Unselect (Click)

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

//Select the Checkbox
checkBox.click();

//Check the Element Selected Status
status = checkBox.isSelected();
System.out.println(status);//false

driver.close();

6) Handling Button

  • 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”)).isDisplayed();
System.out.println(displayStatus);

boolean enabledStatus = driver.findElement(By.xpath(“//*[@id=’identifierNext’]/content/span”)).isEnabled();
System.out.println(enabledStatus);

driver.findElement(By.xpath(“//*[@id=’identifierNext’]/content/span”)).click();

7) Radio Button

  • 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();

//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);

Selenium Video
Selenium WebDriver Tutorial for Beginners

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);

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

  • 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=6rqlkhhibc3d4pvkdt6fafs4s1”);

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

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 e = dropdown.getOptions();
System.out.println(e.size());
driver.close();