gpt4 book ai didi

c# - Selenium C# 中的显式等待不起作用。怎么了?

转载 作者:行者123 更新时间:2023-11-30 14:24:23 28 4
gpt4 key购买 nike

所以我遇到了显式等待的问题。我不想使用 Thread.Sleep()。这是一个简单的测试,它打开一个页面然后返回和前进。加载此页面大约需要 2-3 秒,我想以动态方式(测试)执行此操作。希望我不会太困惑。我做了很多研究,但没有任何效果,也许我做错了什么。 (我正在使用 Resharper 运行单元测试)

这里我也有解决办法:

https://www.dropbox.com/s/1k5vjc5czvmdd3u/ConsoleApplication2.rar?dl=0

我正在使用 FindElement 方法的扩展,因此我相信调用此方法并自行等待会很容易。我在解决方案中评论了一些解释。如果有人给我一些帮助,我将不胜感激。 (抱歉,英语不是很完美)。

using System;
using System.Threading;
using ConsoleApplication2.Extensions;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace ConsoleApplication2
{
class UnitTest1
{
private IWebDriver driver = new ChromeDriver();
[SetUp]
public void Initialize()
{
driver.Url = "some url";
Thread.Sleep(3500);
// driver.Manage().Timeouts().ImplicitlyWait(TimeSpan(20));

}

[Test]
public void CheckBackForward()
{
//Go to first page in Online Help
driver.FindElement(By.XPath("//span/ul/li/span/a")).Click();
// So if I am commenting this Thread.Sleep
// it will throw an exception at the extension method at line 13 at "@by"
// I've also checked this without extension method but still the same problem. It doesn'w wait at all, it will throw this
// exception as soon as FindElement method is called.
// I know that I shouldn't mix explicit waits with implicit ones.
//Thread.Sleep(1500);
//Store title
var title_text = driver.FindElement(By.XPath("//div[@id='shellAreaContent']/div/div[2]/ol/li[3]/span"),60).Text;
//Check if Back is enabled
Assert.IsTrue(driver.FindElement(By.XPath("//div[3]/a/span")).Enabled);
//Go back
driver.FindElement(By.XPath("//div[3]/a/span")).Click();
//Check if Forward is enabled
Assert.IsTrue(driver.FindElement(By.XPath("//div[4]/a/span")).Enabled);
//Go forward
driver.FindElement(By.XPath("//div[4]/a/span")).Click();
//Store title
var title_text2 = driver.FindElement(By.XPath("//div[@id='shellAreaContent']/div/div[2]/ol/li[3]/span")).Text;
//Check if you are on the same page
Assert.AreEqual(title_text2,title_text);
}



[TearDown]
public void EndTest()
{
driver.Quit();
}

}
}

这是扩展:

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

namespace ConsoleApplication2.Extensions
{
public static class Extension
{
public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)
{
if (timeoutInSeconds <= 0) return driver.FindElement(@by);
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(drv => drv.FindElement(@by));
}



}
}

最佳答案

我已经使用 Selenium 编码 6 个多月了,我遇到了和你一样的问题。我创建了这个扩展方法,它每次都对我有用。

代码的作用是:在 20 秒内,它每 500 毫秒检查一次该元素是否出现在页面上。如果 20 秒后仍未找到,则会抛出异常。这将帮助您进行动态等待。

  public static class SeleniumExtensionMethods {
public static WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
public static void SafeClick(this IWebElement webElement) {
try {
wait.Until(ExpectedConditions.ElementToBeClickable(webElement)).Click();
} catch (TargetInvocationException ex) {
Console.WriteLine(ex.InnerException);
}

}

然后替换你的这段代码:

driver.FindElement(By.XPath("//span/ul/li/span/a")).Click();

与:

IWebElement x = driver.FindElement(By.XPath("//span/ul/li/span/a"));
x.SafeClick();

关于c# - Selenium C# 中的显式等待不起作用。怎么了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41286593/

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