Python + Selenium: Reload a page after a timeout on WebDriverWait call
I am looking for a proper way to implement below in Python Selenium
- Load a page
- Wait for a certain period of time(e.g 30 secs) for a button to be clickable (by calling WebDriverWait)
- If got TimeoutException, reload the page again, i.e. goto step 1)
url = 'https://...' driver = webdriver.Chrome('./chromedriver') try: driver.get(url) wait = WebDriverWait(driver, 30) element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'button'))) except TimeoutException as e: <reload the url again>
You can create function that will auto call with refresh if element is not found.
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 os driver = webdriver.Chrome(executable_path =os.path.abspath(os.getcwd()) + "/chromedriver") driver.get("https://selenium-python.readthedocs.io/waits.html") def refresh(): try: element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CLASS_NAME, "button")) ) except: driver.refresh() refresh() refresh()
Python (programming language), Python Syntax compared to other programming languages � Python was designed for readability, and has some similarities to the English language with influence� Browse & Discover Thousands of Computers & Internet Book Titles, for Less.
You can get list of elements with
explicit wait with presence of elements in Dom with
class name. If list of element is empty then you can refresh page.
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 url = 'https://...' driver = webdriver.Chrome('./chromedriver') driver.get(url) wait = WebDriverWait(driver, 30) if len(wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME,'button'))))==0 : driver.refresh()
Python Tutorial, Online. On-demand. Learn at your own pace by doing interactive coding exercises.
Introduction to Python, Python knows the usual control flow statements that other languages speak — if, for, while and range — with some of its own twists, of course. More control flow tools in Python 3 Python is a programming language that lets you work quickly and integrate systems more effectively.
The Python web site provides a Python Package Index (also known as the Cheese Shop, a reference to the Monty Python script of that name). There is also a search page for a number of sources of Python-related information.
Python is an interpreted, high-level, general-purpose programming language.Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace.
Python is a programming language. Python can be used on a server to create web applications. Start learning Python now »