Bri*_*ter 5 c# selenium webdriver frames selenium-webdriver
我正在尝试等待Selenium切换变化的帧,然后再等待另一个元素。即
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")));
Run Code Online (Sandbox Code Playgroud)
如果我在Thread.Sleep(1000);第二次等待之前进行一次简单的操作,它的功能就很好,但是如果没有这样做,我会得到以下错误:
'unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot find context with specified id"}
enter code here
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来等待框架上下文切换完成,然后再等待填充该框架中的元素?
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"));
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
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")));
Run Code Online (Sandbox Code Playgroud)
Here you can find a detailed discussion on Ways to deal with #document under iframe