我想知道如何将Selenium
中C#
的这两种通用方法转换为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);
}
答案 0 :(得分:0)
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");
}
}
答案 1 :(得分:0)
这是一个非常直接的转换:Func
=&gt; java.util.function.Function
IWebDriver
=&gt; WebDriver
(对于网络元素相同)typeof
=&gt; .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));
}