Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python selenium:等待元素可单击但不工作_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python selenium:等待元素可单击但不工作

Python selenium:等待元素可单击但不工作,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我正在尝试使用expected_conditions.element_可单击,但它似乎不起作用。在大约30%的运行中,我仍然看到“元素…在点上不可单击”错误 以下是完整的错误消息: selenium.common.exceptions.WebDriverException:消息:未知 错误:元素。。。在点(621337)处不可单击。另外 元素将收到单击:。。。 (会话信息:chrome=60.0.3112.90) (驱动程序信息:chromedriver=2.26.436421(6c1a3ab4

我正在尝试使用expected_conditions.element_可单击,但它似乎不起作用。在大约30%的运行中,我仍然看到“元素…在点上不可单击”错误

以下是完整的错误消息:

selenium.common.exceptions.WebDriverException:消息:未知 错误:元素。。。在点(621337)处不可单击。另外 元素将收到单击:。。。 (会话信息:chrome=60.0.3112.90) (驱动程序信息:chromedriver=2.26.436421(6c1a3ab469ad86fd49c8d97ede4a6b96a49ca5f6),平台=Mac OS X 10.12.6 x86_64)

以下是我正在使用的代码:

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)
更新:

所以现在这真的很奇怪。即使我添加了几个固定的等待时间,它仍然会偶尔抛出错误消息。以下是拨打电话的代码:

  sleep(5)
  elem = utils.wait_for_element_to_be_clickable('button.ant-btn-primary')
  sleep(5)
  elem.click()

错误消息中说明了问题

元素。。。在点(621337)处不可单击。其他元素将收到单击:


问题是某些元素(您从错误消息中删除了这些元素的详细信息)妨碍了。。。在您尝试单击的元素顶部。在许多情况下,这是一些对话框或其他UI元素的阻碍。如何应对这取决于形势。如果是打开的对话框,请将其关闭。如果您关闭了某个对话框,但代码运行速度很快,请等待该对话框的某些UI元素不可见(该对话框已关闭且不再可见),然后尝试单击。通常,它只是读取被阻塞元素的HTML,在DOM中找到它,并找出如何等待它消失,等等。

最终创建了自己的自定义函数来处理异常并执行重试:

""" 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')
“”“依赖于异常的自定义可单击等待函数。”“”
def自定义_等待_可单击_和_单击(选择器,尝试次数=5):
计数=0
当计数<尝试次数时:
尝试:
等待(1)
#如果超时,这将抛出一个异常,这正是我们想要的。
#我们只想在确认后点击它
#selenium认为它是可见和可点击的。
elem=等待元素可点击(选择器)
元素单击()
返回元素
除了WebDriverException作为e:
如果('在str(e)中的点不可点击]:
打印('单击按钮重试')
计数=计数+1
其他:
提高e
引发TimeoutException('自定义\u等待\u可单击超时')

人们会认为函数在进行isclick确定时会考虑到这一点。我最终创建了自己的函数来捕获异常并执行重试。您的函数工作正常!但是我必须把等待改为时间。睡眠。你能解释一下等待是从哪里来的吗?