mercredi 22 février 2017

Waiting for element to appear in selenium using XPath C#

When I run my selenium tests, they fail as the element on the page has not appeared yet. I have used the below method to wait for the element to appear but it doesn't seem to work.

public bool ControlExistsByXPath(string controlXPath, IWebDriver driver)
    {
        try
        {
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(2));
            var element = wait.Until(d =>
            {
                var control = d.FindElement(By.XPath(controlXPath));
                return control;
            });
            return element.Displayed;
        }
        catch (Exception)
        {
            return false;
        }
    }

For some of the tests this seems to work, like in the one below, but not others.

[When(@"I click on the ""(.*)"" attribute")]
    public void WhenIClickOnTheAttribute(string attribute)
    {
        driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15));
        if (helper.ControlExistsByLinkText(attribute, driver))
        {
            driver.FindElement(By.LinkText(attribute)).Click();
        }
    }

Aucun commentaire:

Enregistrer un commentaire