我想如何将这 2 个通用方法转换为 C#
中的Selenium
Java
版本,因为我没有任何Java
经验:
public static IWebElement WaitAndFindElement(Func<IWebDriver, IWebElement> expectedCondtions, int timeoutInSeconds)
{
WebDriverWait webDriverWait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
webDriverWait.IgnoreExceptionTypes(typeof(NoSuchElementException));
return webDriverWait.Until(expectedCondtions);
}
public static ReadOnlyCollection<IWebElement> WaitAndFindElements(Func<IWebDriver, ReadOnlyCollection<IWebElement>> expectedCondtions, int timeoutInSeconds)
{
WebDriverWait webDriverWait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
webDriverWait.IgnoreExceptionTypes(typeof(NoSuchElementException));
return webDriverWait.Until(expectedCondtions);
}
第一个: 等待元素变为可点击
状态public static void WaitAndClick(WebElement elementToBeClicked) throws InterruptedException, IOException {
try {
WebDriverWait wait = new WebDriverWait(driver, 20);
WebDriverWait wait1 = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOf(elementToBeClicked));
wait1.until(ExpectedConditions.elementToBeClickable(elementToBeClicked));
elementToBeClicked.click();
} catch (Exception e)
{
Logger_Info("Element not clicked yet. waiting some more for " + elementToBeClicked);
if (waitCounter < 3) {
waitCounter++;
WaitAndClick(elementToBeClicked);
}
waitCounter = 0;
}
第二个:
// Wait for an element to become visible
public static void WaitUntilVisible(WebDriver driver, WebElement elementToBeClicked) throws InterruptedException, IOException {
try {
WebDriverWait wait = new WebDriverWait(driver, 20);
WebDriverWait wait1 = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOf(elementToBeClicked));
if (!elementToBeClicked.isDisplayed()) {
Logger_Info("Element not visible yet. waiting some more for " + elementToBeClicked);
if (waitCounter < 3) {
waitCounter++;
WaitUntilVisible(elementToBeClicked);
}
waitCounter = 0;
}
} catch (Exception e)
{
Logger_Info("Handling exception");
}
}
这是一个
非常简单的转换:Func
=> java.util.function.Function
IWebDriver
=> WebDriver
(与 Web 元素相同)typeof
=> .class
。完全:
public static WebElement waitAndFindElement(Function<WebDriver, WebElement> expectedCondtions, int timeoutInSeconds) {
WebDriverWait webDriverWait = new WebDriverWait(driver, timeoutInSeconds).ignoring(NoSuchElementException.class);
return webDriverWait.until(expectedCondtions);
}
public static List<WebElement> waitAndFindElements(Function<WebDriver, List<WebElement>> expectedCondtions, int timeoutInSeconds)
{
WebDriverWait webDriverWait = new WebDriverWait(driver, timeoutInSeconds).ignoring(NoSuchElementException.class);
return Collections.unmodifiableList(webDriverWait.until(expectedCondtions));
}