1

I am following a tutorial and I took the URL from there to try to learn Implicit wait on it. I wrote the below code to click the button on page and then wait 30 seconds for the new element to be visible, before taking the text from the element and confirming it with Assert. The code works fine when i debug but running the test results in failure and the test is also finished in just 6.8 seconds.

driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/dynamic_loading/1");
driver.FindElement(By.XPath("//*[@id='start']/button")).Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
string text = driver.FindElement(By.Id("finish")).Text;
Assert.AreEqual("Hello World!", text);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

1

ImplicitWait is not that effective when interacting with dynamic elements. Instead you need to replace implicit wait with explicit wait.

As an example, to retrieve the text from the element you have to induce WebDriverWait for the ElementIsVisible() and you can use either of the following Locator Strategies:

  • Id:

    string text = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.Id("finish"))).Text;
    Assert.AreEqual("Hello World!", text);
    
  • CssSelector:

    string text = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("div#finish"))).Text;
    Assert.AreEqual("Hello World!", text);
    
  • XPath:

    string text = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//div[@id='div#finish']"))).Text;
    Assert.AreEqual("Hello World!", text);
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • @WaseemAbbas Just now updated the answer as per your existing program. – undetected Selenium Dec 28 '20 at 13:07
  • I found code for explicit wait from the tutorial I have been following. But the code doesn't work. Technically its the same as what you wrote but written differently. What i don't understand is why is that the code you wrote is working but the other one isn't: **WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); IWebElement ele = wait.Until((result) => { return result.FindElement(By.Id("finish")); });** – Waseem Abbas Dec 28 '20 at 14:01