Skip to content

Waits

Using wait methods you will be able to synchronize parts of the process waiting for something before performing a certain action.

Wait

You can make the process wait/sleep for an interval of time in milliseconds.

# Wait for 5 seconds.
bot.wait(5000)

# This will have the same effect as using wait.
bot.sleep(5000)
// Wait for 5 seconds.
wait(5000);

// This will have the same effect as using wait.
sleep(5000);

Waiting for Files

In addition to waiting for process situations, you can also wait for some files. These methods can be useful when you need to wait for a file to be saved or available on disk.

Wait for File

If you have the path of a specific file, you can use it directly in the wait method.

# Waiting for the file to be available for up to 60 seconds.
if bot.wait_for_file(path="/my_directory/files/document.pdf", timeout=60000):
    print("The file is available!")
// Waiting for the file to be available for up to 60 seconds.
if(waitForFile("/my_directory/files/document.pdf", 60000)) {
    System.out.println("The file is available!");
}

Wait for New File

In cases where you don't have or don't know the specific path of the file, you can use the wait method based on the file extension.

# Waiting for a new file without the file path.
# In the first parameter you can set the folder where the file is expected.
filepath = bot.wait_for_new_file(path=bot.download_folder_path, file_extension=".pdf", timeout=60000)
print(f"New created file => {filepath}")
// Waiting for a new file without the file path.
// In the first parameter you can set the folder where the file is expected.
String filePath = waitForNewFile(getDownloadPath(), ".pdf", 0, 60000);
System.out.println("New created file => " + filePath);

Wait for Downloads

You can wait until all the downloads are finished before closing the browser or proceeding with your automation steps.

# Wait for all downloads to be finished up to 100 seconds.
# This method will replace the current page with the downloads page.
bot.wait_for_downloads(timeout=100000)
// Wait for all downloads to be finished up to 100 seconds.
// This method will replace the current page with the downloads page.
waitForDownloads(100000);

Wait for New Page

In cases where a new page opens, you can do a treatment to wait for this page properly before proceeding with the process.

# Starting the process.
bot.browse("https://documentation.botcity.dev/")

print("Opening a new page")

# Using the method as a context manager.
with bot.wait_for_new_page(waiting_time=10000, activate=True):
    # The code inside the 'with' context will be responsible for opening a new page.
    btn_new_page = bot.find_element('/html/body/div[3]/nav/div/ul/li[5]/a', By.XPATH)
    btn_new_page.click()

# When exiting the 'with' block the new page is already loaded and activated.
print("New page open, continuing the process...")
// Starting the process.
browse("https://documentation.botcity.dev/");

System.out.println("Opening a new page");

// Passing the code block responsible for opening the new page.
waitForNewPage(true, 10000, new Runnable() {
    @Override
    public void run() {
        WebElement btnNewPage = findElement(By.xpath("/html/body/div[3]/nav/div/ul/li[5]/a"));
        btnNewPage.click();
    }
});

System.out.println("New page open, continuing the process...");

Wait for Element Visibility

You can wait until an element is visible or hidden on the page.

# Finding an element.
login = bot.find_element("login", By.ID)

# Waiting until it is visible on the page.
bot.wait_for_element_visibility(element=login, visible=True, waiting_time=10000)
// Finding an element.
WebElement login = findElement(By.id("login"));

// Waiting until it is visible on the page.
waitForElementVisibilitiy(login, true, 10000);

Wait for Stale Element

In addition, it is also possible to wait until the element becomes stale (outdated).

# Finding an element.
button = bot.find_element("button", By.ID)

# Monitoring this element.
bot.wait_for_stale_element(element=button, timeout=10000)
// Finding an element.
WebElement button = findElement(By.id("button"));

// Monitoring this element.
waitForStaleElement(button, 10000);