错误“其他元素将获得点击".在Python中 [英] Error "Other element would receive the click" in Python

查看:140
本文介绍了错误“其他元素将获得点击".在Python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图单击这样的链接:

<div class="loading" style="display:none;">
<p class="btn blue"><span>さらに表示</span></p>
<a href="javascript:void(0);" onclick="get_more();"></a>
</div>

我使用了这段代码:

element = WebDriverWait(driver, 30).until(lambda x: x.find_element_by_css_selector(".btn.blue"))  # @UnusedVariable
element.click()

我遇到这样的错误,该怎么解决?

selenium.common.exceptions.WebDriverException: Message: unknown error: Element <p class="btn blue">...</p> is not clickable at point (391, 577). Other element would receive the click: <a href="javascript:void(0);" onclick="get_more();"></a>
(Session info: headless chrome=69.0.3497.100)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 6.1.7601 SP1 x86_64)

解决方案

您尝试单击的元素已被其他元素覆盖,以便其他元素获得点击,而不是实际元素.

可能有以下几种可能导致实际元素未被点击:

  • 案例1 .可以说它是否是一个加载器,它是在元素获得加载并在一段时间后变得不可见时出现的.

    解决方案:在这里,您必须等待加载程序变得不可见,然后才能单击实际元素

      from selenium.webdriver.support import expected_conditions as EC
      wait = WebDriverWait(driver, 10)
      element = wait.until(EC.invisibility_of_element_located((By.ID, 'loader_element_id')))
      element_button = wait.until(EC.element_to_be_clickable((By.ID, 'your_button_id')))
      element_button.click()
    

  • 案例2 .实际元素在浏览器维度中不可见,并被某些叠加元素覆盖.

    解决方案:在这里,您需要滚动到所需的元素,然后必须执行点击操作

      from selenium.webdriver.common.action_chains import ActionChains
    
      element = driver.find_element_by_id("your_element_id")
    
      actions = ActionChains(driver)
      actions.move_to_element(element).perform()
    

    或使用可以像

    <div class="loading" style="display:none;">
    <p class="btn blue"><span>さらに表示</span></p>
    <a href="javascript:void(0);" onclick="get_more();"></a>
    </div>
    

    一样使用:

      driver.execute_script("arguments[0].scrollIntoView();", element)
    

    或使用JavaScript执行点击.

      driver.execute_script("arguments[0].click();", element) 
    

注意:如果需要,请根据Python语法进行必要的更正.

I tried to click a link like this:

<div class="loading" style="display:none;">
<p class="btn blue"><span>さらに表示</span></p>
<a href="javascript:void(0);" onclick="get_more();"></a>
</div>

and I used this code:

element = WebDriverWait(driver, 30).until(lambda x: x.find_element_by_css_selector(".btn.blue"))  # @UnusedVariable
element.click()

I got an error like this, what can I do to solve it?

selenium.common.exceptions.WebDriverException: Message: unknown error: Element <p class="btn blue">...</p> is not clickable at point (391, 577). Other element would receive the click: <a href="javascript:void(0);" onclick="get_more();"></a>
(Session info: headless chrome=69.0.3497.100)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 6.1.7601 SP1 x86_64)

解决方案

Element on which you are trying to click has been covered by some other element so that other element getting the click instead of the actual element.

There may be following possibilities that actual element not getting clicked:

  • Case 1. lets say if its a loader which comes while your element getting load and get invisible after some time.

    Solution: Here you have to wait until the loader get invisible and then have to perform click on actual element

      from selenium.webdriver.support import expected_conditions as EC
      wait = WebDriverWait(driver, 10)
      element = wait.until(EC.invisibility_of_element_located((By.ID, 'loader_element_id')))
      element_button = wait.until(EC.element_to_be_clickable((By.ID, 'your_button_id')))
      element_button.click()
    

  • Case 2. actual element is not visible within browser dimension and covered by some overlay element.

    Solution: Here you need to scroll to the required element and then have to perform the click

      from selenium.webdriver.common.action_chains import ActionChains
    
      element = driver.find_element_by_id("your_element_id")
    
      actions = ActionChains(driver)
      actions.move_to_element(element).perform()
    

    OR use can use execute_script like :

      driver.execute_script("arguments[0].scrollIntoView();", element)
    

    OR perform the click using JavaScript.

      driver.execute_script("arguments[0].click();", element) 
    

Note: Please make necessary correction as per Python syntax if require.

这篇关于错误“其他元素将获得点击".在Python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆