我在VS中有一个程序,使用C#和Selenium将数据输入到某个网站的文本框中。我在网上找到了这种方法,它等到元素存在。
尝试从另一个类调用WaitUntilElementExists方法,但它不起作用。不知道我是否错过了什么。如果你能帮助我,谢谢。提前感谢!
public static void InputTextbox(IWebDriver wDriver, string sElement, string sValue, int iIndex)
{
//calling WaitUntilElementExists method
var wait = new WebDriverWait(wDriver, TimeSpan.FromSeconds(20));
wait.Until(ExpectedConditions.ElementExists(By.Name("value")));
//input text box
}
public static class WaitForElement
{
public static IWebElement WaitUntilElementExists(this IWebDriver wDriver, By elementLocator, int iTimeout)
{
try
{
if (iTimeout > 0)
{
var wait = new WebDriverWait(wDriver, TimeSpan.FromSeconds(iTimeout));
return wait.Until(ExpectedConditions.ElementExists(elementLocator));
}
return wDriver.FindElement(elementLocator);
}
catch (NoSuchElementException)
{
Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
throw;
}
}
}
您有两个选择:
使用WaitUntilElementExists
作为常规静态方法
IWebElement element = WaitForElement.WaitUntilElementExists(driver, By.Name("value"), 20);
或者将其用作扩展方法,使用IWebDriver
实例调用它
IWebElement element = driver.WaitUntilElementExists(By.Name("value"), 20);