Issue
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
Solution
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
Answered By - undetected Selenium
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.