gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:42:48 24 4
gpt4 key购买 nike

我已经在 C# 中启动了一个 Selenium 项目。尝试等待页面完成加载,然后才进行下一步操作。

我的代码是这样的:

 loginPage.GoToLoginPage();
loginPage.LoginAs(TestCase.Username, TestCase.Password);
loginPage.SelectRole(TestCase.Orgunit);
loginPage.AcceptRole();

在 loginPage.SelectRole(TestCase.Orgunit) 中:

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

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

1. _browserInstance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));

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

您将如何克服这个障碍?

最佳答案

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

第一选择 将检查元素属性,直到达到超时。我已到达以下属性,确认它在页面上可用:

存在 - 期望检查元素是否存在于页面的 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;
}
}

可见性 - 期望检查元素是否存在于页面的 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;
}
}

可点击 - 检查元素是否可见并启用以便您可以点击它的期望。

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

第二个选择适用于触发器对象(例如菜单项)在单击后不再附加到 DOM 的情况。当元素上的单击操作将触发重定向到另一个页面时,通常就是这种情况。在这种情况下,检查 StalenessOf(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;
}
}

关于c# - 如何让 webDriver 等待页面加载(C# Selenium 项目),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43203243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com