因此,我创建了这个通用的find元素函数:
public static IWebElement FindElement(IWebDriver driver, Func<IWebDriver, IWebElement> expectedCondtions, int timeoutInSeconds)
{
WebDriverWait webDriverWait = CreateWebDriverWait(driver, finder,timeoutInSeconds);
webDriverWait.IgnoreExceptionTypes(typeof(NoSuchElementException));
return webDriverWait.Until(expectedCondtions);
}
public static ReadOnlyCollection<IWebElement> FindElements(IWebDriver driver, Func<IWebDriver, ReadOnlyCollection<IWebElement>> expectedCondtions, int timeoutInSeconds)
{
WebDriverWait webDriverWait = CreateWebDriverWait(driver, finder, timeoutInSeconds);
webDriverWait.IgnoreExceptionTypes(typeof(NoSuchElementException));
return webDriverWait.Until(expectedCondtions);
}
private static WebDriverWait CreateWebDriverWait(IWebDriver driver, IWebElement finder, int timeoutInSeconds)
{
WebDriverWait webDriverWait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
webDriverWait.IgnoreExceptionTypes(typeof(NoSuchElementException));
return webDriverWait;
}
用法:
IWebElement element=
WaitAndFindElement(
driver,
ExpectedConditions.ElementIsVisible(By.CssSelector("...")),
120);
现在,我想添加一个选项,以查找也不使用element
的driver
。例如,我想从另一个driver.FindElement
中搜索元素,而不是element
IWebElemen element = ...
element.FindElement...
因此,我想将我的函数签名更改为:
IWebElement FindElement(IWebDriver driver,Func<IWebDriver, IWebElement> expectedCondtions, int timeoutInSeconds)
至:
IWebElement FindElement(IWebDriver driver, IWebElement finder, Func<IWebDriver, IWebElement> expectedCondtions, int timeoutInSeconds)
如果finder
为null,我希望使用driver.FindElement
进行搜索。否则:finder.FindElement
所以我的问题是如何做到这一点?
发布于 2017-04-14 02:51:23
Class WebElementFinder
{
public static IWebElement FindElement(ISearchContext sc, By locator, Func<IWebElement, bool> elementCondition = null, int timeOutInceconds = 20)
{
DefaultWait<ISearchContext> wait = new DefaultWait<ISearchContext>(sc);
wait.Timeout = TimeSpan.FromSeconds(timeOutInceconds);
wait.PollingInterval = TimeSpan.FromSeconds(3);
wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
return wait.Until(x => GetElement(x, locator, elementCondition));
}
private static IWebElement GetElement(ISearchContext sc, By locator, Func<IWebElement, bool> elementCondition = null)
{
IWebElement webElement = sc.FindElement(locator);
if(elementCondition != null)
{
if (elementCondition(webElement))
return webElement;
else
return null;
}
else
{
return webElement;
}
}
}
用法:
Func<IWebElement, bool> isElementVisible = (webElement) => webElement.Displayed;
var element = FindElement(driver, By.Id("name_10"), isElementVisible);
发布于 2017-04-13 14:19:19
这很好:我在工作中遇到过类似的情况,这就是我在C#中所做的:
IWebElement FindElement(IWebDriver driver, Func<IWebDriver, IWebElement> expectedCondtions, int timeoutInSeconds, IWebElement finder = null)
这意味着"finder“的默认值为null,您可以使用或不指定它来调用函数。(可以向所有方法添加具有默认值的此参数)
然后,您可以在函数中简单地执行一个简单的if语句,以确定如何找到元素。
if(finder != null)
{
//use finder instead of driver and return
}
//Otherwise use driver
发布于 2017-04-13 21:18:56
我认为如果你想把它变得通用的话,你不需要让它变得复杂。下面是解决这个问题的代码
Class WebElementFinder
{
private static IWebElement FindElement(ISearchContext sc, By locator, int timeOutInceconds = 20)
{
DefaultWait<ISearchContext> wait = new DefaultWait<ISearchContext>(sc);
wait.Timeout = TimeSpan.FromSeconds(timeOutInceconds);
wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
return wait.Until(x => GetElement(x, locator));
}
private static IWebElement GetElement(ISearchContext sc, By locator)
{
return sc.FindElement(locator);
}
}
让我解释一下这段代码的某些部分,它将帮助您理解您提到的代码的问题。
现在回到我的代码片段。我使用了DefaultWait,它既可以用于WebDriver,也可以用于IWebElement。两者都是从ISearchContext驱动的。按照您在文章中的要求,定位器将帮助在驱动程序/ IWebElement对象上查找web元素。
https://stackoverflow.com/questions/43401676
复制相似问题