0

I Need to wait for an element to be NOT visible and found a couple of solutions for this, but none of them seems to work for me.

I think the problem is because I'm using PageObject Models. but I'm not complete sure.

 public static void WaitForElementToBeInvisible(this Browser browser, IWebElement element, int seconds = 30)
        {
            var wait = new WebDriverWait(browser.Driver, new TimeSpan(0, 0, seconds));
            wait.Until(ExpectedConditions.InvisibilityOfElementLocated(element));
        }

But it returns error cannot convert from IWebElement to Selenium.By

Thanks in advance

Buaban
  • 5,029
  • 1
  • 17
  • 33
Dymond
  • 2,158
  • 7
  • 45
  • 80
  • Can you add function calling code as well? – Murthi Jun 19 '17 at 17:09
  • From this exception, you are passing Iwebelement instead of By locator. – Murthi Jun 19 '17 at 17:12
  • 1
    Hello Dymond, I have done ton of research on this and coding with selenium c# since 6+ months and from my knowledge this is not possible. Some things about the Page Object design pattern don't make sense and you chould try to do this with a `By` object. – Happy Bird Jun 19 '17 at 23:20

4 Answers4

1

Short answer, this is not possible. The developers of Selenium have decided that there are no useful use cases for this.

If By locator is possible, you can use:

  public static void WaitUntilInvisible(this By locator)
        {
            try
            {
                if (Driver.FindElement(locator).Displayed)
                {
                    wait.Until(ExpectedConditions.ElementIsVisible(locator));
                }
        }

See also: Trying to convert IWebElement into a By element

Happy Bird
  • 1,012
  • 2
  • 11
  • 29
1

Your code is incorrect. It calls InvisibilityOfElementLocated which doesn't receive IWebElement as a parameter. See my example below.

public static bool WaitForElementToBeInvisible(this IWebElement element, int timeoutSecond = 10)
{
    IWait<IWebElement> wait = new DefaultWait<IWebElement>(element);
    wait.Timeout = TimeSpan.FromSeconds(timeoutSecond);
    wait.PollingInterval = TimeSpan.FromMilliseconds(300);
    try
    {
        wait.Until(!element.Displayed);
    }
    catch (WebDriverTimeoutException)
    {
        return false;
    }

    return true;
}

IWebElement div = driver.FindElement(By.Id("id"));
var result = div.WaitForElementToBeInvisible(5);
Buaban
  • 5,029
  • 1
  • 17
  • 33
0

Thanks for all the answer, but solved it like this.

public static void WaitUntilInvisible(this Browser browser, By element, int seconds = 30)
        {
            var wait = new WebDriverWait(browser.Driver, new TimeSpan(0, 0, seconds));
            wait.Until(ExpectedConditions.InvisibilityOfElementLocated(element));
        }

Then used as By locator

By LoadingIcon = By.XPath(".//*[contains(@class, 'loading')]");

works like a charm :)

Dymond
  • 2,158
  • 7
  • 45
  • 80
0
do { Playback.Wait(100); } while (_driver.FindElements(By.Id("elementId")).Count() > 0);
Roger Perkins
  • 376
  • 2
  • 9