Last modified: Nov 22, 2023 By Alexander Williams
Python Selenium: Wait Until Element is Visible (XPath)
Example 1: Use WebDriverWait for Visibility
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage
driver.get('https://example.com')
# Wait until the element with XPath is visible (adjust timeout as needed)
xpath = '//button[@id="example-button"]'
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, xpath))
)
# Perform actions on the visible element
element.click()
# Close the browser
driver.quit()
Output:
# No specific output. The script waits until the element is visible and then performs an action.
Example 2: Use Expected Conditions with XPath
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage
driver.get('https://example.com')
# Define the XPath of the element to wait for
xpath = '//input[@name="example-input"]'
# Wait until the element is visible (adjust timeout as needed)
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, xpath))
)
# Perform actions on the visible element
element.send_keys('Hello, Selenium!')
# Close the browser
driver.quit()
Output:
# No specific output. The script waits until the element is visible and then performs an action.
Example 3: Use Implicit Wait with XPath
from selenium import webdriver
# Create a Chrome driver with implicit wait
driver = webdriver.Chrome()
driver.implicitly_wait(10) # Set implicit wait time in seconds
# Navigate to a webpage
driver.get('https://example.com')
# Find the element with XPath
xpath = '//div[@class="example-div"]'
element = driver.find_element(By.XPATH, xpath)
# Perform actions on the visible element
element.click()
# Close the browser
driver.quit()
Output:
# No specific output. The script implicitly waits until the element is visible and then performs an action.
Example 4: Use Custom Wait Function with XPath
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# Custom wait function for element visibility with XPath
def wait_for_element_visibility(driver, xpath, timeout=10):
start_time = time.time()
while time.time() - start_time < timeout:
try:
element = driver.find_element(By.XPATH, xpath)
if element.is_displayed():
return element
except Exception as e:
pass
time.sleep(0.5) # Adjust sleep duration as needed
raise TimeoutError(f"Element with XPath '{xpath}' not visible after {timeout} seconds.")
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage
driver.get('https://example.com')
# Use the custom wait function to wait for element visibility
xpath = '//div[@id="custom-element"]'
element = wait_for_element_visibility(driver, xpath)
# Perform actions on the visible element
element.click()
# Close the browser
driver.quit()
Output:
# No specific output. The script waits until the element is visible and then performs an action.
Example 5: Use Expected Conditions with Element to be Clickable
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage
driver.get('https://example.com')
# Wait until the element with XPath is clickable (adjust timeout as needed)
xpath = '//a[@id="clickable-link"]'
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, xpath))
)
# Perform actions on the clickable element
element.click()
# Close the browser
driver.quit()
Output:
# No specific output. The script waits until the element is clickable and then performs an action.
Example 6: Use JavaScript Executor for Visibility Check
from selenium import webdriver
# Custom JavaScript to check if an element with XPath is visible
visibility_check_script = """
var element = arguments[0];
var rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
"""
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage
driver.get('https://example.com')
# Find the element with XPath
xpath = '//img[@id="visible-image"]'
element = driver.find_element('xpath', xpath)
# Use JavaScript to check element visibility
is_visible = driver.execute_script(visibility_check_script, element)
# Perform actions based on visibility
if is_visible:
print("Element is visible. Perform actions.")
# Perform actions on the visible element
else:
print("Element is not visible. Wait or handle accordingly.")
# Close the browser
driver.quit()
Output:
# The script prints whether the element is visible or not.