Skip to content
Advertisement

auto refresh page while element is not clickable (python)

Objective: Find an appointment

Code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import numpy as np

options = Options()
options.add_argument("--start-maximized")

import time
start = time.process_time()

time.sleep(3)
s = Service(path)
driver = webdriver.Chrome(options=options, service=s)

page = 'https://service.berlin.de/terminvereinbarung/termin/tag.php?
        termin=1&dienstleister=122231&anliegen[]=326798&herkunft=1'
driver.get(page)

time.sleep(5)
driver.refresh()

While the code works, I would like to extend it by making it do auto-refresh every 5 second interval until the element on 9 September 2022 is clickable.

enter image description here

I am thinking of something like

if wait.until(EC.element_to_be_clickable((By.XPATH, 
                '//*[@id="layout-grid__area--maincontent"] 
                /div/div/div[2]/div[2]/div/div/div[5]/div/div[2] 
                /div[1]/table/tbody/tr[2]/td[5]'))).click() is False:
    time.sleep(5)
    driver.refresh()
else:
     break

but the second part of the code does not work.

An example of a clickable date is on Nov 4. enter image description here

Update: Added a while True loop

i=0
while (True):
    element = driver.find_element(by=By.XPATH, value='//*[@id="layout-grid__area--maincontent"]
                                       /div/div/div[2]/div[2]/div/div/div[5]/div/div[2]
                                       /div[1]/table/tbody/tr[2]/td[5]')
    i+=1
    if "nichtbuchbar" in element.get_attribute("class"):
        time.sleep(5)
        driver.refresh()
    print(f'The {i}'th try has failed')

I am not sure whether the code above does the job properly, because it seems that every new line is printed in less than 1 second, whereas it is supposed to take a 5 second pause before refresh.

Advertisement

Answer

You can check the class name on that element instead, that will give you the same information as is_clickable, would such a function exist (assuming the red ones are not clickable and the white ones are. If you need a blue one, look at the css class on that one and see if that is in the class name).

if "buchbar" not in element.get_attribute("class"):
    time.sleep(5)
    driver.refresh()
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement