27 – Element Handling in Selenium Part – 2
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
…………………………………………………………………….
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();
//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
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(“type”));
driver.findElement(By.name(“username”)).clear();
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();
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/div[1]/div/form/content/section/div/content/div[1]/div/div[2]/div[2]”)).getText();
System.out.println(errorMessage);
//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…
……………………………….
Manual Actions on Link Element
Manual Actions on Link Element
- Check Displayed Status (Visible/Displayed/Exists)
- Check Enabled Status
- Click
- Return Element Name etc…
…………………………………………………………………….