如何等到div层被隐藏

问题描述 投票:0回答:1

我有这个用于点击下拉按钮的代码:

等到被隐藏:

new WebDriverWait(driver, Duration.ofSeconds(timeout)).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[contains(@class, 'ngx-spinner-overlay')]")));
new WebDriverWait(driver, Duration.ofSeconds(timeout)).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.ngx-spinner-overlay")));

点击按钮的代码:

Thread.sleep(4000);
WebDriverWait button2Wait = new WebDriverWait(driver, Duration.ofSeconds(timeout));
WebElement button2 = button2Wait.until(ExpectedConditions.elementToBeClickable(By.xpath(dropDownId)));
new Actions(driver).moveToElement(button2).build().perform();
button2.click();

但有时我会得到例外:

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <mobileweb-value-selector _ngcontent-jkb-c228="" id="equipment" selectorskin="component" property.id="equipmentId" property.name="equipmentLookupCode" _nghost-jkb-c212="" class="ng-star-inserted">...</mobileweb-value-selector> is not clickable at point (694, 368). Other element would receive the click: <div _ngcontent-jkb-c171="" class="ngx-spinner-overlay ng-tns-c171-0 ng-trigger ng-trigger-fadeIn ng-star-inserted ng-animating" style="background-color: rgba(255, 255, 255, 0.75); z-index: 99999; position: fixed;">...</div>
  (Session info: chrome=110.0.5481.177)

你知道我如何实现一个等待直到这个 div 层被隐藏的代码吗?

java selenium-webdriver webdriverwait
1个回答
0
投票

首先,我在您的代码块中没有看到任何错误/问题。

根据规范元素默认点击滚动到视图。所以从一般的角度来看,使用 Actions 在我看来是一种开销。


这个用例

作为最佳尝试,我更喜欢以下代码行:

new WebDriverWait(driver, Duration.ofSeconds(timeout)).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.ngx-spinner-overlay")));
new WebDriverWait(driver, Duration.ofSeconds(timeout)).until(ExpectedConditions.elementToBeClickable(By.xpath(dropDownId))).click();

Incase 尝试失败,使用 Actions 并在链中单击:

new WebDriverWait(driver, Duration.ofSeconds(timeout)).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.ngx-spinner-overlay")));
WebElement button2 = new WebDriverWait(driver, Duration.ofSeconds(timeout)).until(ExpectedConditions.elementToBeClickable(By.xpath(dropDownId)));
new Actions(driver).moveToElement(button2).click(button2).build().perform();

Incase Actions 尝试失败,然后使用 JavascriptExecutor

 中的 
executeScript() 方法,如下所示:

new WebDriverWait(driver, Duration.ofSeconds(timeout)).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.ngx-spinner-overlay")));
WebElement button2 = new WebDriverWait(driver, Duration.ofSeconds(timeout)).until(ExpectedConditions.elementToBeClickable(By.xpath(dropDownId)));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", button2);
© www.soinside.com 2019 - 2024. All rights reserved.