How to wait for a frame to load before locating an element?

谁说胖子不能爱 提交于 2019-11-29 11:05:34

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"));

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.

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")));

Here you can find a detailed discussion on Ways to deal with #document under iframe

San

You can wait for the frame itself to be clickable:

wait2.Until(ExpectedConditions.ElementExists(By.Id("YOURFRAMEID")));

This is how you can do it:

var wait = new WebDriverWait(new SystemClock(), driver, TimeSpan.FromSeconds(timeout), TimeSpan.FromSeconds(sleepInterval)); 
wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt("yourFrameName"); 
driver.SwitchTo().Frame("yourFrameName");

I am not sure which language you're working on. But in C#, you would need to SwitchTo default content first and then switch to the Iframe you're dealing with which is frameA. So here is my suggested code to try:

driver.SwitchTo().DefaultContent();
driver.SwitchTo().Frame(frameA);

Update: Implement a method that waits for element explicitly:

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);
  });
}

And then call the method to wait for your element

WaitForElementExplicitly(Selector: By.Id("elementA"));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!