Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for a frame to load before locating an element?

I'm trying to wait for Selenium to switch changing frames before waiting on another element. I.e.

var wait = new WebDriverWait(driver, 15);
wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.Id("frameA"));

var wait2 = new WebDriverWait(driver, 15);
// wait for element within frameA to exist
wait2.Until(ExpectedConditions.ElementExists(By.Id("elementA")));

If I toss in a simple Thread.Sleep(1000); before the second wait it functions fine, but without that I get the following error:

'unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot find context with specified id"}
    enter code here

Is there a better way to wait for the frame context to switch finishing before waiting for an element within that frame to be populated?

like image 493
Brian von Kuster Avatar asked Apr 05 '18 16:04

Brian von Kuster


People also ask

How do you wait for a frame to load?

We can wait for the iframe to load completely with Selenium webdriver. First of all we need to identify the iframe with the help iframe id, name, number or webelement. Then we shall use the explicit wait concept in synchronization to wait for the iframe to load.

How do you use wait until an element is visible?

Selenium: Waiting Until the Element Is Visiblevar wait = new WebDriverWait(driver, TimeSpan. FromSeconds(20)); As you can see, we give the WebDriverWait object two parameters: the driver itself and a TimeSpan object that represents the timeout for trying to locate the element.

How do you wait for an object in Selenium?

Explicit Wait in SeleniumBy using the Explicit Wait command, the WebDriver is directed to wait until a certain condition occurs before proceeding with executing the code. Setting Explicit Wait is important in cases where there are certain elements that naturally take more time to load.


1 Answers

There are a couple of things you need to consider :

The line of code to switch to the frame looks perfect which doesn't throws any error :

var wait = new WebDriverWait(driver, 15);
wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.Id("frameA"));

In the next line you have tried the ExpectedConditions method ElementExists. As per the API Docs ElementExists Method is defined as :

An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.

Selenium can't interact with elements until the element is visible. Hence you need to use the method ElementIsVisible as follows :

var wait2 = new WebDriverWait(driver, 15);
wait2.Until(ExpectedConditions.ElementIsVisible(By.Id("elementA")));

Here you can find a detailed discussion on Ways to deal with #document under iframe

like image 102
undetected Selenium Avatar answered Sep 28 '22 00:09

undetected Selenium