1

I'm trying to create a bot that auto refreshes and stops whenever the desired element is available to click/visible. I've made the refresh part and the bot stops when it sees the desired element, but I just can't figure out why it doesn't click on the element:/
Error log

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

driver = webdriver.Chrome(executable_path="C:\selenium drivers\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()

while True:
    try:
        element = WebDriverWait(driver, 2).until(EC.presence_of_element_located((driver.find_element_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'))))
        driver.find_element_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').click()
        break
    except TimeoutException:
        driver.refresh()
        continue

2 Answers2

0
  1. I guess you are using non-unique locator so that driver.find_element_by_xpath('xxx') returns you a wrong element so that clicking on it does nothing.
    To fix this issue validate the xxx XPath locator you are using is unique.
  2. Or maybe you trying to click on that element too early so that it gives you ElementClickInterceptedException or something similar.
    To give better answer we need to see your actual code, not xxx if it is possible, including the errors traceback.
Prophet
  • 32,350
  • 22
  • 54
  • 79
0

presence_of_element_located()

presence_of_element_located() takes a locator as an argument but not an element.

So you need to change:

element = WebDriverWait(driver, 2).until(EC.presence_of_element_located((driver.find_element_by_xpath('xxx'))))

as:

element = WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.XPATH, "xxx")))

Ideally, to locate the clickable element and invoke click() on it, you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:

while True:
    try:
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "xxx"))).click()
        break
    except TimeoutException:
        driver.refresh()
        continue
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Your solution worked, the bot will now click on the element its told and if the element isnt present it will refresh until. ty so much:) – LunarBurning Nov 15 '21 at 21:37