如何在定位元素之前等待框架加载?
How to wait for a frame to load before locating an element?
我正在尝试等待 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
在等待框架内的元素被填充之前,是否有更好的方法来等待框架上下文切换完成?
您可以等待框架本身可点击:
wait2.Until(ExpectedConditions.ElementExists(By.Id("YOURFRAMEID")));
我不确定您使用的是哪种语言。但是在 C# 中,您需要首先切换到默认内容,然后切换到您正在处理的 Iframe,即 frameA。所以这是我建议尝试的代码:
driver.SwitchTo().DefaultContent();
driver.SwitchTo().Frame(frameA);
更新:
实现一个显式等待元素的方法:
public void WaitForElementExplicitly(int WaitInMilliSeconds = 3000, By Selector = null)
{
WebDriverWait wait = new WebDriverWait(CommonTestObjects.IWebDriver, TimeSpan.FromSeconds(WaitInMilliSeconds / 1000));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
return d.FindElement(Selector);
});
}
然后调用方法等待你的元素
WaitForElementExplicitly(Selector: By.Id("elementA"));
您可以这样做:
var wait = new WebDriverWait(new SystemClock(), driver, TimeSpan.FromSeconds(timeout), TimeSpan.FromSeconds(sleepInterval));
wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt("yourFrameName");
driver.SwitchTo().Frame("yourFrameName");
您需要考虑几件事:
切换到框架的代码行看起来很完美,不会引发任何错误:
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.
Selenium 在元素可见 之前无法与元素交互。因此,您需要使用 ElementIsVisible
方法,如下所示:
var wait2 = new WebDriverWait(driver, 15);
wait2.Until(ExpectedConditions.ElementIsVisible(By.Id("elementA")));
在这里你可以找到关于
的详细讨论
我正在尝试等待 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
在等待框架内的元素被填充之前,是否有更好的方法来等待框架上下文切换完成?
您可以等待框架本身可点击:
wait2.Until(ExpectedConditions.ElementExists(By.Id("YOURFRAMEID")));
我不确定您使用的是哪种语言。但是在 C# 中,您需要首先切换到默认内容,然后切换到您正在处理的 Iframe,即 frameA。所以这是我建议尝试的代码:
driver.SwitchTo().DefaultContent();
driver.SwitchTo().Frame(frameA);
更新: 实现一个显式等待元素的方法:
public void WaitForElementExplicitly(int WaitInMilliSeconds = 3000, By Selector = null)
{
WebDriverWait wait = new WebDriverWait(CommonTestObjects.IWebDriver, TimeSpan.FromSeconds(WaitInMilliSeconds / 1000));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
return d.FindElement(Selector);
});
}
然后调用方法等待你的元素
WaitForElementExplicitly(Selector: By.Id("elementA"));
您可以这样做:
var wait = new WebDriverWait(new SystemClock(), driver, TimeSpan.FromSeconds(timeout), TimeSpan.FromSeconds(sleepInterval));
wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt("yourFrameName");
driver.SwitchTo().Frame("yourFrameName");
您需要考虑几件事:
切换到框架的代码行看起来很完美,不会引发任何错误:
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.
Selenium 在元素可见 之前无法与元素交互。因此,您需要使用 ElementIsVisible
方法,如下所示:
var wait2 = new WebDriverWait(driver, 15);
wait2.Until(ExpectedConditions.ElementIsVisible(By.Id("elementA")));
在这里你可以找到关于