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

标签 c# selenium selenium-webdriver webdriver frames

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

有没有更好的方法来等待框架上下文切换完成,然后再等待该框架内的元素被填充?

最佳答案

您需要考虑几件事:

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

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

在下一行中,您尝试了 ExpectedConditions 方法 ElementExists。根据 API 文档 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.

Selenium 无法与元素交互,直到元素可见。因此你需要使用方法 ElementIsVisible如下:

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

在这里您可以找到关于 Ways to deal with #document under iframe 的详细讨论。

关于c# - 如何在定位元素之前等待框架加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49677267/

相关文章:

java - 使用 Selenium 3 在 headless phantomjs 2.1.1 中找不到元素

javascript - 如何在 Eclipse 中使用 Selenium 将外部 .js 导入到我的 Java 测试中?

c# - 如何在 WebClient.DownloadFileAsync 上实现超时

C#:重构雷诺数计算器

javascript - 如何用 selenium 重新加载 html DOM,以便新命令定位新节点?

java - appium 的此 session 问题不支持定位器策略 'css selector'

javascript - 从 Python 提交基于 Silverlight 的表单(也许使用 JS?)

Selenium 网格 : Identify WebDriver node where a test failure occured

c# - 客户端 C# 在 asp.net 中给出错误

c# - byte + byte = int...为什么?