Python Selenium – Wait until specific element is visible
We can wait for a specific element in the webpage to be visible, before performing an action on that element, or some other.
To wait for a specific element in the webpage to be visible, using Selenium in Python, you can use Explicit Waits.
Follow these steps to create an explicit wait condition for an element in Selenium.
1. Create a WebDriverWait object
Set up the WebDriverWait instance, specifying the maximum waiting time in seconds (in this case, 10 seconds).
wait = WebDriverWait(driver, 10)
2. Specify the element locator
This is the element for which we are going to wait for it to load.
Get the appropriate locator (e.g., ID, XPath, CSS selector) for the element.
For example, consider the following element locator, where we identify the element by id attribute.
element_locator = (By.ID, 'element_id')
3. Wait for the element to become visible
Now, write the code to wait for the element to become visible.
The visibility_of_element_located() method from ExpectedConditions is used to create a condition that waits for the element to become visible.
For example, consider the following wait condition applied on the element locator. The until() method returns the element located by the given locator, with the applied visibility condition.
element = wait.until(EC.visibility_of_element_located(element_locator))
In the following program, we load the URL https://pythonexamples.org/tmp/selenium/index-18.html.
<html> <body> <h3>My Form</h3> <form action=""> <label for="firstname">First name:</label> <input type="text" id="firstname" name="firstname"><br><br> <label for="lastname">Last name:</label> <input type="text" id="lastname" name="lastname"><br><br> <input type="submit" value="Submit"> </form> </body> </html>
And wait for the element (input text field) with id=”firstname” to load. Once the element is loaded, we shall enter some text into the input text field, and take a screenshot of that element.
from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Setup chrome driver driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install())) # Navigate to the url driver.get('https://pythonexamples.org/tmp/selenium/index-18.html') # Set the maximum waiting time in seconds wait = WebDriverWait(driver, 10) # Adjust the waiting time (e.g., 10 seconds) # Find the element you want to wait for (using any appropriate locator) element_locator = (By.ID, 'firstname') # Replace 'element_id' with the actual ID of the element # Wait until the element is visible element = wait.until(EC.visibility_of_element_located(element_locator)) # Perform actions on the element after it becomes visible element.send_keys('Apple Banana') screenshot = element.screenshot_as_png # Save the screenshot to a file with open('element_screenshot.png', 'wb') as file: file.write(screenshot) # Close the WebDriver driver.quit()
In this Python Selenium tutorial, we have seen how to introduce an Explicit Wait to wait for a specific element in the web page to be visible, with examples.