小编典典

如何在定位元素之前等待框架加载?

selenium

我正在尝试等待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")));

如果我在Thread.Sleep(1000);第二次等待之前进行一次简单的处理,它的功能就很好,但是如果没有这样做,我会收到以下错误消息:

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

在等待填充该框架中的元素之前,是否有更好的方法来等待框架上下文切换完成?


阅读 415

收藏
2020-06-26

共1个答案

小编典典

您需要考虑以下几点:

切换到框架的代码行看起来很完美,不会引发任何错误:

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

在下一行中,您尝试了 ExpectedConditions 方法 ElementExists 。根据 API Docs
ElementExists
方法的定义为:

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

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