Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

java - How to pause Selenium execution when progress bar is shown

I have a Angular SPA application which I would like to automate. When this progress bar is shown:

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

I would like to pause globally the execution of Java code. Is it possible to pause Selenium somehow if this div is visible?

question from:https://stackoverflow.com/questions/65853707/how-to-pause-selenium-execution-when-progress-bar-is-shown

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

invisibilityOf()

invisibilityOf(WebElement element) is defined as:

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

An expectation for checking the element to be invisible

Here the expectation is that, the element must be present as well as visible as a pre-condition and the method will wait for the element to be invisible. At this point it is worth to mention that as the argument is of type WebElement, findElement(By by) have to successfully locate the element as a pre-condition. Hence NoSuchElementException can't be ignored.


invisibilityOfElementLocated()

invisibilityOfElementLocated(By locator) is defined as:

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.

Here the expectation is clearly either the element is already invisible or not present in the HTML DOM. In this case the primary mission is the absence of the element which may occur even before the ExpectedCondition is invoked or during the timespan while the ExpectedCondition is active. So here we need to ignore NoSuchElementException as a mandatory measure.


This usecase

To pause the execution of your program you need to induce WebDriverWait and you can use either of the following Locator Strategies:

  • Using invisibilityOf():

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...