2020年7月27日 星期一

selenium: wait for element to be clickable not working

I'm attempting to use expected_conditions.element_to_be_clickable but it doesn't appear to be working. I'm still seeing "Element...is not clickable at point" errors in about 30% of the runs.

Here's the full error message:

selenium.common.exceptions.WebDriverException: Message: unknown error: Element ... is not clickable at point (621, 337). Other element would receive the click: ... (Session info: chrome=60.0.3112.90) (Driver info: chromedriver=2.26.436421 (6c1a3ab469ad86fd49c8d97ede4a6b96a49ca5f6),platform=Mac OS X 10.12.6 x86_64)

Here's the code I'm working with:

def wait_for_element_to_be_clickable(selector, timeout=10):
  global driver
  wd_wait = WebDriverWait(driver, timeout)

  wd_wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector)),
    'waiting for element to be clickable ' + selector)
  print ('WAITING')
  return driver.find_element_by_css_selector(selector)


""" custom clickable wait function that relies on exceptions. """
def custom_wait_clickable_and_click(selector, attempts=5):
  count = 0
  while count < attempts:
    try:
      wait(1)
      # This will throw an exception if it times out, which is what we want.
      # We only want to start trying to click it once we've confirmed that
      # selenium thinks it's visible and clickable.
      elem = wait_for_element_to_be_clickable(selector)
      elem.click()
      return elem

    except WebDriverException as e:
      if ('is not clickable at point' in str(e)):
        print('Retrying clicking on button.')
        count = count + 1
      else:
        raise e

  raise TimeoutException('custom_wait_clickable timed out')

沒有留言:

張貼留言