gpt4 book ai didi

javascript - selenium ElementNotVisibleException 无法捕获隐藏元素

转载 作者:行者123 更新时间:2023-11-28 01:46:38 25 4
gpt4 key购买 nike

我目前正在尝试使用 Selenium 进行一些用户界面测试,我遇到了这个很好的方法(不确定我从哪里得到它......),它应该处理不存在的元素和隐藏元素......

问题在于第二个问题:即使元素未显示/隐藏(可见性:隐藏),该方法仍不断返回“true”

public boolean elementExists(By locator, WebDriver driver) {
WebElement foo = null;
try {
foo = this.getElementByLocator(locator, 10, driver);
} catch (TimeoutException te) {
System.out
.println("Timeout - Dieses Element konnte nicht gefunden werden: "
+ locator.toString());
return false;
}
catch (ElementNotVisibleException env) {
System.out
.println("Dieses Element wurde gefunden, ist aber nicht sichtbar: "
+ locator.toString());
return false;
}
if (foo == null ) {
return false;
} else {
return true;
}
}

public WebElement getElementByLocator(By locator, int timeout,
WebDriver driver) {
System.out.println("Rufe Methode getElementByLocator: "
+ locator.toString());
int interval = 5;
if (timeout <= 20)
interval = 3;
if (timeout <= 10)
interval = 2;
if (timeout <= 4)
interval = 1;
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(timeout, TimeUnit.SECONDS)
.pollingEvery(interval, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class,
StaleElementReferenceException.class);
WebElement we = wait.until(ExpectedConditions
.presenceOfElementLocated(locator));
return we;
}

有人可以告诉我如何修改它以便能够识别隐藏的现有元素吗?提前致谢!

最佳答案

ExpectedConditions.presenceOfElementLocated

检查页面的 DOM 上是否存在元素。这并不一定意味着该元素是可见的。

尝试使用visibilityOfElementLocated代替。这会检查某个元素是否存在于页面的 DOM 上并且可见。可见性是指元素不仅能显示,而且高度和宽度都大于0。

更多内容 here

catch (ElementNotVisibleException env) {

我不认为这会被归入你的案子中。无论如何,如果您要与它交互并且该元素将被隐藏 - 这将被抛出,但不会在查找时抛出。

编辑:为什么这么多代码却没有什么好处?这具有相同的作用:

   public boolean elementExists(By locator, WebDriver driver){
return (new WebDriverWait(driver, 60)
.ignoring(NoSuchElementException.class)
.ignoring(StaleElementReferenceException.class)
.until(ExpectedConditions.visibilityOfElementLocated(locator))) != null;
}

关于javascript - selenium ElementNotVisibleException 无法捕获隐藏元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20216289/

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