如何在定位元素之前等待框架加载? [英] How to wait for a frame to load before locating an element?
问题描述
我试图等待Selenium切换变化的帧,然后再等待另一个元素.即
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")));
如果我在第二次等待之前扔了一个简单的Thread.Sleep(1000);
,它就可以正常工作,但是如果没有,我会得到以下错误:
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?
推荐答案
您需要考虑以下几点:
切换到框架的代码行看起来很完美,不会引发任何错误:
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"));
在下一行中,您尝试了 ExpectedConditions 方法 ElementExists .根据 API文档 > ElementExists
方法定义为:
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.
硒不能与元素交互,直到元素可见.因此,您需要使用方法 ElementIsVisible
如下:
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")));
在这里您可以找到有关交易方式的详细讨论在iframe下使用#document
Here you can find a detailed discussion on Ways to deal with #document under iframe
这篇关于如何在定位元素之前等待框架加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!