Selenium From Window Handle
Selenium From Window Handle
Characteristics
We can move this Pop Up.
We can Inspect this Pop Up.
This Pop Up will have Minimum, Maximise and Close button along with the Address Bar.
Solutions
To handle Child Window Pop we use getWindowHandles() and switchTo().window() statement.
Note
Window Handle:- Address of the browser present on the desktop is known as Window
Handle.(session id)
In order to retrieve it we use getWindowHandle() or getWindowHandles().
Write a Script to get the Window Handle and print it on the console for google.com.
O/P-
Cdwindow-8BF45AF68ED4E8CE1130A84D26A16EAD
getWindowHandle getWindowHandles
It returns the address of Current Browser It returns address of all the browsers
Return Type of getWindowHandle is String Return type of getWindowHandles is
Set<String>
Write a Script to print all the address of all the browsers opened by naukri.com
Write a Script to print all the Titles of the browsers opened by naukri.com
BACKSIDE OF COPY
NoSuchWindowException – Selenium/Unchecked
We get this Exception when ever we try perform any action on the browser which is
already closed or we pass invalid Window Handle to window().
07/12/2021
Write a Script to close this specific Browser in naukri.com
ASSIGNMENT
Write to close all the browsers except the specific browser.
Write a script to close all the browsers except the parent browsers.
for(String wh:allwh)
{
driver.switchTo().window(wh);
if(!(pwh.equals(wh)))
{
driver.close();
}
}
}
Summary Of the Pop Ups
POP UP SOLUTIONS
JavaScript/Alert Pop Up By using driver.switchTo().alert()-
accept,dismiss,getText
Hidden Division/ Calender Pop Up By using findElement()
File Upload Pop Up By using
BrowserButton.sendKeys(absolutePath).
File Download Pop Up (Firefox) By using Robot class(for Chrome Browser no
need to handle)
Print Pop Up() By using Robot Class (for Chrome Browser-
findElement())
Notification Pop Up By using addArguments() of BrowserOptions
class to change the settings
Authentication Pop Up By sending the Username and Password in
along with the URL in get().
Child Window/Browser Pop Up By using getWindowHandles() and
driver.switchTo().window() statement.
HANDLING TABS
Tab is also treated as New Window in Selenium.
Here we are going to handle the tab in the same way as we handle child window Pop or
Child Window Pop Up.
Write a Script to count the no. Of tabs present in the browser after clicking in
actiTime Link.
ASSIGNMENT
Write a script to print the window handles of all the tabs present in the browser after
clicking actitime link.
public class PrintAllTabActitime
{
static
{
System.setProperty("webdriver.chrome.driver",
"./driver/chromedriver.exe");
}
public static void main(String[] args)
{
WebDriver driver = new ChromeDriver();
driver.get("https://demo.actitime.com/");
driver.findElement(By.linkText("actiTIME Inc.")).click();
Set<String> allwh = driver.getWindowHandles();
int count = allwh.size();
System.out.println(count);
for(String wh:allwh)
{
driver.switchTo().window(wh);
String title = driver.getTitle();
System.out.println(title);
}
driver.quit();
}
ASSIGNMENT
Write a Script to AUTOMATE the following scenario.:
1) Go to to demo.actitime.com
2) Login to the application
3) Go to Help drop down Icon(?) and select “about your actitime”.
4) Click on Read Service Agreement on the Pop Up.
5) Print all the Tab Headings present in the new Tab.
driver.findElement(By.xpath("(//div[@class='menuCellDiv']/table/tbody/tr[2]/td/di
v/div[4]/div/div/div/div)[1]")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//a[.='About your actiTIME']")).click();
driver.findElement(By.xpath("//a[.='Read Service Agreement']")).click();
Set<String> alltabwh = driver.getWindowHandles();
for(String tabwh:alltabwh)
{
driver.switchTo().window(tabwh);
String title = driver.getTitle();
System.out.println(title);
}
driver.quit();
}
08/12/2021
Write a script to close the Child tab 1st then the Parent tab by using Iterator.
Actions class is mainly used for Mouse Actions and it has parametrized constructor
where it takes WebDriver as an argument.
Whenever we call any methods of Actions class, we have to use perform() at the
last.
All the methods present of Actions class is an example for Method Overloading.
Example-
URL:- http://www.dhtmlgoodies.com/submitted-scripts/i-google-like-drag-drop/index.html
Program:-
jqueryui.com
09/12/2021
AUTOMATE the following scenario:-
1) Go to vtiger.com
2) Mouse Hover to ‘Resources’ and select customers in the Drop Down Menu.
3) Click Double click on ‘Read Full Story’ button.
4) Check whether ‘HackerEarth’ page is displayed or not?
Write A script to enter jsp in T2 Text Box and qsp in T1 Text Box
driver.get("file:///D:/WORKSTATION/SELENIUM/Automation/htmlcode/page1.html");
driver.switchTo().frame(0);
driver.findElement(By.id("t2")).sendKeys("jsp");
driver.switchTo().parentFrame();
driver.findElement(By.id("t1")).sendKeys("qsp");
}
NoSuchFrameException SELENIUM/Unchecked:-
We get this exception whenever we pass invalid argument to frame().
Page1.html
Page 2.html
Page 3.html
OK
yes
driver.get("file:///D:/WORKSTATION/SELENIUM/Automation/htmlcode/page1.html");
//switch the control inside the frame using string i.e id
driver.switchTo().frame("f1");
driver.findElement(By.id("t2")).sendKeys("a");
driver.switchTo().parentFrame();
driver.findElement(By.id("t1")).sendKeys("b");
WebElement frm = driver.findElement(By.xpath("//iframe"));
//switch to frame using WebElement
driver.switchTo().frame(frm);
driver.findElement(By.id("t2")).sendKeys("c");
driver.switchTo().defaultContent();
driver.findElement(By.id("t1")).sendKeys("d");
}
}
ASSIGNMENT
Write a Script to AUTOMATE the following scenario:-
1) Open the Browser
2) Go to makemytrip.com
3) Select Bangalore in ‘From’ Textbox.
4) Select Goa in ‘To’ Textbox.
5) Select the date 31st Dec, 2021 in the Depart on Calender Pop Up.
6) Click on ‘Search Flights’ button.
7) Capture and print all the flights along with departure time.
a.doubleClick(driver.findElement(By.xpath("//img[@alt='MakeMyTrip']"))).perform();
Thread.sleep(2000);
a.doubleClick(driver.findElement(By.xpath("//a[.='Search']"))).perform();
List<WebElement> allflights =
driver.findElements(By.xpath("//div[@class='listingCard']/div[2]/div[1]/div/span"));
List<WebElement> allprice =
driver.findElements(By.xpath("//div[@class='listingCard']/div[2]/div[4]/div/div/p"));
List<WebElement> depttime =
driver.findElements(By.xpath("//div[@class='listingCard']/div[2]/div[2]/label/div/div/d
iv[1]/div[1]/p/span"));
int count = allflights.size();
for(int i=0;i<count;i++)
{
String flightName = allflights.get(i).getText();
String price = allprice.get(i).getText();
String time = depttime.get(i).getText();
System.out.println(flightName+"------------>"+time+"-----------
->"+price);
}
driver.close();
In Selenium, we don’t have a method to handle disabled Elements and Scroll Bar(By
using Java).
In order to handle we use executeScript().
ExecuteScript is declared in JavaScriptExecutor Interface and implemented in
RemoteWebDriver Class. Since, we already Upcasted to Browser Specific classes to
WebDriver interface, this method will be hidden. In order to access this method, we
should downcast it to RemoteWebDriver or Typecast the object to JavaScriptExecutor.
If the elements are disabled and if we try to perform action on disabled elements,
it will throw ElementNotInteractableException.
Example:-
UN:<input type=“text” id=’d1’/> <br>
PW:<input type=“text” id=“d2” disabled/> <br>
<input type=“button” value = “Login”/>
driver.get("file:///D:/WORKSTATION/SELENIUM/Automation/htmlcode/Disabled.html");
driver.findElement(By.id("d1")).sendKeys("admin");
RemoteWebDriver r = (RemoteWebDriver) driver;
r.executeScript("document.getElementById('d2').value='manager'");
}
Search Context
TypeCasting
WebDriver JavascriptExecutor
Down Casting
RemoteWebDriver
ChromeDriver
NOTE
In order to validate JavaScript statement in the browser, Inspect the Element and
click on Console Tab present in the Developer Toolbar and type the JavaScript
statement.
j.executeScript("window.scrollTo(0,0)");
Backside of Notecopy
ElementNotInteractableException (Selenium/Unchecked):-
We get this exception whenever we try to perform action on the disabled elements().
JavascriptException (Selenium/Unchecked):-
We get this exception whenever there is a mistake in syntax of JavaScript statement.