Python - Refresh page until Element is clickable

Viewed 311

I'm trying to create this bot that will refresh the page until an element is visible. My problem is that the bot isn't refreshing and I can't seem to figure out why:)

EDIT: The bot refreshes the desired page now until the desired elememt is visible, however the bot wont click the desired element it was looking for, and that I cant seem to figure out why, since the code says "element.click()"

I believe that this is the logs

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


driver = webdriver.Chrome(executable_path="C:\webdrivers\chromedriver.exe")


driver.get("xxx")

driver.maximize_window()


click = driver.find_element_by_xpath('//*[@id="coiPage-1"]/div[2]/div[1]/button[1]')
click.click()

time.sleep(1)

while true:

    if(driver.find_elements_by_xpath('//*[@id="siteContainer"]/div[6]/div/div[3]/div[1]/div[2]/div/div/div[2]/div[2]/div[3]/div[2]/form/button'))
   element.click()
   break
else:
   driver.refresh()
continue
2 Answers

I'm not an expert, but I will try my best to help you. First of all, it seems that your indent is wrong, and you forgot a colon after your if condition. Your True need to start with an uppercase to be recognized as a Boolean, and you should use a try statement instead of an if.

And, you can't click() on a list of WebElement. You need to find ONE element, by using find_element, not find_elements.

If you import TimeoutException from selenium.common.exceptions, you can use it to determine if an element exist.

while True:
    try:
        element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH'//*[@id="siteContainer"]/div[6]/div/div[3]/div[1]/div[2]/div/div/div[2]/div[2]/div[3]/div[2]/form/button')))
        element.click()
        break
    except TimeoutException:
        driver.refresh()
        continue

presence_of_element_located() is the expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible or clickable/interactable.

Ideally to invoke click() on an element you need to you need to induce WebDriverWait for the element_to_be_clickable() which is the expectation for checking an element is visible and enabled such that you can click it as follows:

while True:
    try:
        element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id="siteContainer"]/div[6]/div/div[3]/div[1]/div[2]/div/div/div[2]/div[2]/div[3]/div[2]/form/button")))
        element.click()
        break
    except TimeoutException:
        driver.refresh()
        continue
    

Note: You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
Related