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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With