0

我有一个我想自动化的 Angular SPA 应用程序。当显示此进度条时:

<div _ngcontent-cln-c1="" class="ngx-loading-text center-center" style="top: calc(50% + 60px + 5px); color: white;">Loading...</div>

我想全局暂停 Java 代码的执行。如果这个 div 是可见的,是否可以以某种方式暂停 Selenium?

4

2 回答 2

2

你可以使用 webdriver 等待这个:

WebDriverWait wait = new WebDriverWait(driver,30);


wait.until(ExpectedConditions.stalenessOf(loadingelement)));

或者

WebDriverWait wait = new WebDriverWait(driver,30);


wait.until(ExpectedConditions.invisibilityOf​(loadingelement)));

以上将等到加载元素不可见或陈旧(意味着修改或删除)

您还可以使用:

List<WebElement> elementName = driver.findElements(By.xpath("//div[@class=\"ngx-loading-text center-center\"]"));

while(elementlist.size()){
  Thread.sleep(1000)
  elementName = driver.findElements(By.xpath("//div[@class=\"ngx-loading-text center-center\"]"));
}

上面的代码将检查 findelements 列表是否为空 else wai 1 秒然后尝试再次查找并且循环继续直到 size 为 0

于 2021-01-23T13:45:48.793 回答
0

隐形()

invisibilityOf(WebElement element)定义为:

public static ExpectedCondition<java.lang.Boolean> invisibilityOf(WebElement element)

An expectation for checking the element to be invisible

这里的期望是,元素必须作为先决条件存在并且可见,并且该方法将等待元素不可见。此时值得一提的是,由于参数是WebElement类型,findElement(By by)必须成功定位元素作为前提条件。因此NoSuchElementException不容忽视


invisibilityOfElementLocated()

invisibilityOfElementLocated(By locator)定义为:

public static ExpectedCondition<java.lang.Boolean> invisibilityOfElementLocated(By locator)

An expectation for checking that an element is either invisible or not present on the DOM.

这里的期望显然是元素已经不可见不存在HTML DOM中。在这种情况下,主要任务是元素的缺失,这甚至可能在调用 ExpectedCondition 之前或在ExpectedCondition处于活动状态时发生。所以这里我们需要忽略NoSuchElementException作为强制性措施。


这个用例

要暂停程序的执行,您需要诱导WebDriverWait并且您可以使用以下任一Locator Strategies

  • 使用invisibilityOf()

    new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOf(driver.findElement(By.xpath("//div[@class='ngx-loading-text center-center' and starts-with(., 'Loading')]"))));
    
  • 使用invisibilityOfElementLocated()

    new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@class='ngx-loading-text center-center' and starts-with(., 'Loading')]")));
    
于 2021-01-23T15:02:51.887 回答