gpt4 book ai didi

java - Webdriver 等待文本出现

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

单击下拉列表后,我无法单击列表中的元素?

我创建的方法不起作用。

public static void waitForTextToAppearAndClick(WebDriver driver, WebElement element, String textToAppear) throws InterruptedException{
WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.visibilityOf(element));
WebElement locator = element;
locator.click();

WebElement textToClick = driver.findElement(By.linkText(textToAppear));
wait.until(ExpectedConditions.elementToBeClickable(textToClick));
textToClick.click();
}

使用thread.sleep似乎有效,但我不想使用此方法,任何人都可以推荐一种在我单击主按钮后等待并单击特定文本元素的方法吗?

public static void waitForTextToAppearAndClick(WebDriver driver, WebElement element, String textToAppear) throws InterruptedException{
WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.visibilityOf(element));
WebElement locator = element;
locator.click();

Thread.sleep(3000);
driver.findElement(By.linkText(textToAppear)).click();;

}

请注意,我需要点击 BBQ Sauce,需要点击 BBQ Sauce 时 thread.sleep() 成功

enter image description here

感谢您的帮助

最佳答案

使用您自己的 FluentWait 实现,以便等待单击后出现文本:

Wait wait = new FluentWait<>(this.driver)
.withTimeout(driverTimeoutSeconds, TimeUnit.SECONDS)
.pollingEvery(500, TimeUnit.MILLISECONDS)
.ignoring(StaleElementReferenceException.class)
.ignoring(NoSuchElementException.class)
.ignoring(ElementNotVisibleException.class);

WebElement foo = wait.until(new Function() {
public WebElement apply(WebDriver driver) {
return element.getText().length() > 0;
}
});

关于java - Webdriver 等待文本出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40889642/

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