Switch to Frames in Selenium

Debomita Bhattacharjee
Updated on 10-Jun-2020 15:00:32

5K+ Views

We can switch to frames in Selenium with the help of the following methods −switchTo()defaultContent()This method is for switching to and fro in between frames and parent frames. The focus is shifted to the main page.switchTo().parentFrame()This method is used to switch the control to the parent frame of the current frame.Exampleimport org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class FrameDefault {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       String url = "url with frames";       driver.get(url);   ... Read More

Handle Frames in Selenium

Debomita Bhattacharjee
Updated on 10-Jun-2020 14:58:33

6K+ Views

We can handle frames in Selenium with the help of following methods −switchTo().frame( frameNumber)This method uses the frame id as the parameter. The index of frame id starts from 0. NoSuchFrameException is thrown if the frame is not found.switchTo().frame( frameName)This method uses the frame name as defined by the developer as the parameter. The frame name is considered a String and is enclosed in quotes. NoSuchFrameException is thrown if the frame is not found.switchTo().frame( WebElement)This method uses the webelement as the parameter. NoSuchFrameException is thrown if the frame is not found. StaleElementReferenceException if the frame is no longer active.Exampleimport org.openqa.selenium.By; ... Read More

Handle Popup Windows in Selenium

Debomita Bhattacharjee
Updated on 10-Jun-2020 14:54:55

2K+ Views

Selenium has getWindowHandles() method , which returns all the window handle ids for all the open windows. This is stored in Set data structure in String data types.In order to navigate to a specific window, we need to traverse to the window we want to access with the iterator() method then switch to that window.getWindowHandle() method gives the window handle id of the current window Id.Exampleimport org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; import java.util.Set; import java.util.Iterator; import org.testng.annotations.Test; public class WindowHandles{    @Test    public void windowHandle() throws Exception {       ... Read More

Handle Web-Based Alerts in Selenium

Debomita Bhattacharjee
Updated on 10-Jun-2020 14:49:12

1K+ Views

Selenium WebDriver gives multiple APIs to handle pop ups or alerts with the help of Alert interface.dismiss()This will cancel the button for alert.accept()This will accept the button for alert.getText()This will extract the text on alert.sendKeys()This will enter text on the alert box.ExampleSyntax with code snippet −// Alert    Alert a = driver.switchTo().alert();    // Extract alert message.    String msg= driver.switchTo().alert().getText();    // print the message on console    System.out.println(msg);    // entering text on alert box    a .sendkeys(“Testing”);    // alert accept    a.accept()    // alert dismiss       a.dismiss()

Fluent Wait in Selenium: What It Performs

Debomita Bhattacharjee
Updated on 10-Jun-2020 14:38:47

515 Views

Fluent wait is a dynamic wait which makes the driver pause for a condition which is checked at a frequency before throwing an exception. The element is searched in DOM not constantly but at a regular interval of time.For example, if the wait is for 5 seconds, FluentWait monitors the DOM at regular intervals (defined by polling during time). In FluentWait, customized wait methods based on conditions need to be built.Syntax −Wait w = new FluentWait< WebDriver >(driver) .withTimeout (10, SECONDS) .pollingEvery (2, SECONDS) .ignoring (NoSuchElementException.class)Exampleimport org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.support.ui.Wait; import ... Read More

What Does Explicit Wait Perform in Selenium

Debomita Bhattacharjee
Updated on 10-Jun-2020 14:36:27

472 Views

Explicit waits are applied to a specific element in the web page. It shall pause the execution till the condition is satisfied. Explicit wait is also a dynamic one since if the wait time is fifteen seconds and the conditions (like waiting for an element to be clickable, visible or selectable and so on) are satisfied before this specified time, the control will move to the next step.Explicit wait is more customizable since we can set it up for the condition. The listof some of the expected conditions for explicit waits are listed below −textToBePresentInElement()Syntaxw.until(ExpectedConditions.textToBePresentInElement(By.id(““), “Tutorialspoint”));textToBeClickable()Syntaxw.until(ExpectedConditions.textToBeClickable(By.id(““)));alertisPresent()Syntaxw.until(ExpectedConditions.alertisPresent())= null);frameToBeAvailableAndSwitchToIt()Syntaxw.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id(““)));Explicit is complex in ... Read More

What Does Implicit Wait Perform

Debomita Bhattacharjee
Updated on 10-Jun-2020 13:52:48

662 Views

Implicit is the default waiting time for each test step in our execution. Thus if we keep an implicit wait of ten seconds, each test step will wait for that amount of time for an action to take place and then move to the next step.Implicit wait is a dynamic wait which means if the wait time is ten seconds and the web element on which the next action is to be taken is available at the fifth second, then control will immediately move to the next test step rather than waiting for the full ten seconds.However if the element ... Read More

Different Types of Wait in Selenium

Debomita Bhattacharjee
Updated on 10-Jun-2020 13:51:04

806 Views

The different types wait available in Selenium are listed below −Implicit waitThis is one of dynamic waits in Selenium with the Syntax as −driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);Explicit waitThis is one of dynamic waits in Selenium with the Syntax as −WebDriverWait w = new WebDriverWait(driver, ); w.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("")));Fluent waitThis is one of dynamic waits in Selenium with the Syntax as −Wait w = new FluentWait(driver).withTimeout(Duration.ofSeconds(30)) .pollingEvery(Duration.ofSeconds(3)).ignoring(NoSuchElementException.class);Static waitThis is used to pause the execution for a specified amount of time.Exampleimport org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; public class ThreadWait {    public static void main(String[] args) throws InterruptedException ... Read More

Set the Size of the Browser Window in Selenium

Debomita Bhattacharjee
Updated on 10-Jun-2020 13:46:08

4K+ Views

We can set the size of the browser window by the following methods −getSize() methodJavascript executorExampleWith setSize() method.import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; public class BrowserDimension {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       String url = "https://www.tutorialspoint.com/index.htm";       driver.get(url);       // maximize the browser       driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);       // fetching the current window size with getSize()       System.out.println(driver.manage().window().getSize());       //Create object of Dimensions ... Read More

Perform Scrolling Action on Page in Selenium

Debomita Bhattacharjee
Updated on 10-Jun-2020 13:44:08

1K+ Views

We can perform the following actions with respect to scrolling in Selenium −Vertical scrollingThe scrolling down to a specific pixel.JavascriptExecutor j = (JavascriptExecutor) driver; // scroll down by 1500 pixel with coordinates 0 and 1500 in x, y axes j.executeScript("window.scrollBy(0, 1500)");The scrolling down to the bottom of the page.JavascriptExecutor j = (JavascriptExecutor) driver; // scroll down the bottom of page js.executeScript("window.scrollTo(0, document.body.scrollHeight)");ExampleFor vertical scroll down till the element is visible.import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class ScrollDownVisible {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");   ... Read More

Advertisements