如何让webDriver等待页面加载(C#Selenium项目)

cla*_*u84 16 c# pageload ui-automation selenium-firefoxdriver selenium-webdriver

我在C#开始了一个Selenium项目.尝试等待页面完成加载,然后才进行下一步操作.

我的代码看起来像这样:

 loginPage.GoToLoginPage();
        loginPage.LoginAs(TestCase.Username, TestCase.Password);
        loginPage.SelectRole(TestCase.Orgunit);
        loginPage.AcceptRole();
Run Code Online (Sandbox Code Playgroud)

在loginPage.SelectRole(TestCase.Orgunit)中:

 RoleHierachyLabel = CommonsBasePage.Driver.FindElement(By.XPath("//span[contains(text(), " + role + ")]"));
 RoleHierachyLabel.Click();
 RoleLoginButton.Click();
Run Code Online (Sandbox Code Playgroud)

我搜索元素RoleHierachyLabel.我一直在尝试使用多种方式等待页面加载或搜索元素属性允许一些超时:

1. _browserInstance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
Run Code Online (Sandbox Code Playgroud)
2. public static bool WaitUntilElementIsPresent(RemoteWebDriver driver, By by, int timeout = 5)
    {
        for (var i = 0; i < timeout; i++)
        {
            if (driver.ElementExists(by)) return true;
        }
        return false;
    }
Run Code Online (Sandbox Code Playgroud)

你会如何解决这个障碍?

cla*_*u84 23

我一直在寻找替代品,我已经选择了以下版本.所有都使用显式超时的定义等待,并且基于第一种情况下的元素属性和第二种情况下的元素陈旧性.

首选是检查元素属性,直到达到超时.我已经到达以下属性,确认它在页面上可用:

存在 - 期望检查页面的DOM上是否存在元素.这并不一定意味着该元素是可见的.

//this will not wait for page to load
Assert.True(Driver.FindElement(By elementLocator).Enabled)

//this will search for the element until a timeout is reached
public static IWebElement WaitUntilElementExists(By elementLocator, int timeout = 10)
    {
        try
        {
            var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout));
            return wait.Until(ExpectedConditions.ElementExists(elementLocator));
        }
        catch (NoSuchElementException)
        {
            Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
            throw;
        }
    }
Run Code Online (Sandbox Code Playgroud)

可见性 - 期望检查页面的DOM上是否存在元素并且可见.可见性意味着元素不仅会显示,而且高度和宽度也会大于0.

//this will not wait for page to load
Assert.True(Driver.FindElement(By elementLocator).Displayed)

//this will search for the element until a timeout is reached
public static IWebElement WaitUntilElementVisible(By elementLocator, int timeout = 10)
    {
        try
        {
            var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout));
            return wait.Until(ExpectedConditions.ElementIsVisible(elementLocator));
        }
        catch (NoSuchElementException)
        {
            Console.WriteLine("Element with locator: '" + elementLocator + "' was not found.");
            throw;
        }
    }
Run Code Online (Sandbox Code Playgroud)

可点击 - 可以看到并启用检查元素的期望,以便您可以单击它.

//this will not wait for page to load
//both properties need to be true in order for element to be clickable
Assert.True(Driver.FindElement(By elementLocator).Enabled)
Assert.True(Driver.FindElement(By elementLocator).Displayed)

//this will search for the element until a timeout is reached
public static IWebElement WaitUntilElementClickable(By elementLocator, int timeout = 10)
    {
        try
        {
            var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout));
            return wait.Until(ExpectedConditions.ElementToBeClickable(elementLocator));
        }
        catch (NoSuchElementException)
        {
            Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
            throw;
        }
    }
Run Code Online (Sandbox Code Playgroud)

当触发对象(例如菜单项)在单击后不再附加到DOM时,将应用第二个选项.通常情况下,元素上的单击操作将触发重定向到另一个页面.在这种情况下,检查StalenessOf(element)是有用的,其中element是被单击以触发重定向到新页面的项目.

public static void ClickAndWaitForPageToLoad(By elementLocator, int timeout = 10)
    {
        try
        {
            var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout));
            var element = Driver.FindElement(elementLocator);
            element.Click();
            wait.Until(ExpectedConditions.StalenessOf(element));
        }
        catch (NoSuchElementException)
        {
            Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
            throw;
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • _ExpectedConditions_ 已过时。可以使用 lambda 代替: `Wait.Until(x =&gt; x.FindElement(By.Id("xxx")));` Wait.Until() 默认忽略 ‵NoSuchElementException`。 (3认同)

Jos*_*osh 6

driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(5);

另外,请参阅此答案

  • 你应该解释一下这到底是做什么的。例如:这与“ImplicitWait”有何不同? (2认同)