我想用TimeSpan
结构代替int
,而不是写它是关于什么单位的时间。如何转换?
public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)
{
if (timeoutInSeconds > 0)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(drv => drv.FindElement(by));
}
return driver.FindElement(by);
}
}
使用例子:
var element = webDriver.FindElement(By.XPath(@"//h1[@class='m-t30'][contains(.,'My Profile')]"), 15);
将int值更改为TimeSpan和可选参数,因为我看到您希望基于提供的超时控制流
public static IWebElement FindElement(this IWebDriver driver, By by, TimeSpan? timespan = null)
{
if (timespan is not null)
{
var wait = new WebDriverWait(driver, timespan.Value);
return wait.Until(drv => drv.FindElement(by));
}
return driver.FindElement(by);
}
用法:
没有等待,只有FindElement
var element = webDriver.FindElement(By.XPath(@"//h1[@class='m-t30'][contains(.,'My Profile')]"));
等待时间长达30秒
var element = webDriver.FindElement(By.XPath(@"//h1[@class='m-t30'][contains(.,'My Profile')]"), TimeSpan.FromSeconds(30));