selenium questions code example

Example 1: selenium interview questions

How to switch between multiple windows in Selenium?
Ans. Selenium has driver.getWindowHandles() and driver.switchTo().window({windowHandleName}) commands to work with multiple windows.

The getWindowHandles() command returns a list of ids corresponding to each window and on passing a particular window handle to the driver.switchTo().window({windowHandleName}) command, we can switch control/focus to that particular window.

for (String windowHandle : driver.getWindowHandles()) {
     driver.switchTo().window(handle);
}

Example 2: selenium interview questions 2019

// Set up the JavaScript object
JavascriptExecutor jscript = (JavascriptExecutor) webdriver;
// Read the site title
String strTitle = (String)jscript.executeScript("return document.title");
System.out.println("Webpage Title: " + strTitle);

Example 3: selenium interview questions 2019

// Set up the JS object
JavascriptExecutor jscript = (JavascriptExecutor)webdriver;
// Issue command to enter the text
jscript.executeScript("document.getElementById('textbox').value = 'Some Text';");