kokei
kokei

Reputation: 11

Python + Selenium: Reload a page after a timeout on WebDriverWait call

I am looking for a proper way to implement below in Python Selenium

  1. Load a page
  2. Wait for a certain period of time(e.g 30 secs) for a button to be clickable (by calling WebDriverWait)
  3. 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>

Upvotes: 1

Views: 3484

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193058

To perform the following tasks:

  1. Load a page
  2. Wait for a certain period of time(e.g 30 secs) for a button to be clickable (by calling WebDriverWait)
  3. If got TimeoutException, reload the page again, i.e. goto step 1)

You can use the following Locator Strategy. For the sake of demonstration, I will consider an element which is not available on Google Search Home Page:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    while True:
        try:
            driver.get("https://www.google.com/")
            WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "kokei")))
            print("Button found")
            break
        except TimeoutException:
            print("Button not found ... reloading page")
            continue
    # perform your remaining steps here on successfully finding the clickable element
    driver.quit()
    
  • Console Output:

    Button not found ... reloading page
    Button not found ... reloading page
    Button not found ... reloading page
    

Upvotes: 1

Muzzamil
Muzzamil

Reputation: 2881

You can get list of elements with explicit wait with presence of elements in Dom with button as 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()

Upvotes: 0

Zaraki Kenpachi
Zaraki Kenpachi

Reputation: 5730

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()

Upvotes: 2

Related Questions