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
CodePudding user response:
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
CodePudding user response:
- 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 thexxx
XPath locator you are using is unique. - 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, notxxx
if it is possible, including the errors traceback.