gpt4 book ai didi

java - Java 中的通用 FindElement 方法

转载 作者:太空宇宙 更新时间:2023-11-04 11:51:27 25 4
gpt4 key购买 nike

我想知道如何将 C# 中的 Selenium 的这 2 个通用方法转换为 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");
}
}

关于java - Java 中的通用 FindElement 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41791102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com