【发布时间】:2013-09-02 13:59:07
【问题描述】:
我想创建一个通用函数来查找特定项目?
我做这个 C# 代码 (Src : Use Explicit Waits in Selenium WebDriver Correctly)
IWebDriver driver = new FirefoxDriver();
driver.Url = "http://somedomain/url_that_delays_loading";
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
try
{
return d.FindElement(By.Id("someDynamicElement"));
}
catch
{
return null;
}
});
我会替换 wait.Until((....); 使用通用函数。
如何定义一个通用的 By 选择器来查找特定项目?
这里是内置 Selenium2 选择器的列表:
ClassName
CssSelector
Id
LinkText
PartialLinkText
Name
TagName
XPath
例如:
IWebElement myDynamicElement = WaitForElement(????) // for exemple By.TagName = "Test"
public static IWebElement WaitForElement(**By selector**)
{
wait.Until<IWebElement>((d) =>
{
try
{
return d.FindElement(**By selector**);
}
catch
{
return null;
}
});
}
【问题讨论】:
标签: c# selenium selenium-webdriver