Selenium 可点击元素点击了错误的位置?

问题描述 投票:0回答:3

我们正在等待由 xpath 标识的按钮可通过以下方式单击:

ExpectedConditions.elementToBeClickable()

然后在 Selenium 中点击这个按钮执行。

然后我们得到一个错误:

another element would receive the click at position (x, y).

在页面加载期间,该按钮可能会在页面上稍微移动,而其旁边的其他按钮正在加载。

为什么 Selenium 会报告按钮可点击,然后却无法点击?我明白这就是这个条件的目的。此执行发生在同一行中。

我们如何解决这个问题?

selenium selenium-webdriver automated-tests selenium-chromedriver click
3个回答
1
投票

想象一下您想要与一个按钮交互,而该按钮位于页面中间。

案例1:

您不会通过自动化脚本以全屏模式打开浏览器。 那么它的位置就不会在页面中间了。

只需介绍一下就可以解决这个问题

driver.maximize_window()

案例2:

也许在自动化脚本中,一些

pop up
ads
出现,并且确实将按钮隐藏在后台。在这种情况下,你可能会遇到
another element would receive the click at position (x, y).

解决方案:您应该确定它们是什么类型的

pop up/Alert/ads
并单独处理它们,然后只有您应该与预期的按钮进行交互。

案例3:

也可能会发生这种情况,因为您可能已经打开(比方说日历来选择开始/结束日期),然后由于日历视图下方的 Web 元素将无法被 selenium 访问,并且只有日历视图才会获得咔哒声。 [这应该可以回答你的问题]

一般情况下,我在 selenium 脚本中使用

actions class
JS
inject js code

如果您将 Selenium 与 Python 一起使用,那么您可以使用

  1. 使用

    ActionChains

    ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "CSS here")))).click().perform()
    
  2. 使用

    execute_script

    wait = WebDriverWait(driver, 20)
    button= wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "CSS here")))
    driver.execute_script("arguments[0].click();", button)
    

在 Python 中,您还需要以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

0
投票

使用

ExpectedConditions.elementToBeClickable()
允许我们的代码停止程序执行,或冻结线程,直到我们传递给它的条件解决。以一定频率调用该条件,直到等待超时结束。这意味着只要条件返回一个假值,它就会继续尝试和等待。因此,显式等待允许我们等待条件发生,从而同步浏览器及其 DOM Tree 和 WebDriver 脚本之间的状态。

即使使用

ExpectedConditions.elementToBeClickable()
,在所需元素上调用 click() 时也可能会遇到以下错误:

Element <a href="/path1/path2/menu">...</a> is not clickable at point (415, 697). Other element would receive the click: <div class="cc-content">...</div>

WebDriverException: Element is not clickable at point (36, 72). Other element would receive the click

原因及解决方法

发生此错误的原因可能有多种,其中几个原因及其补救措施如下:

  • 虽然该元素位于 Viewport 内,但可能隐藏在 cookie 横幅后面。在这种情况下,您必须先接受 cookie 同意,然后才能与所需的元素交互,如下所示:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("cookieConsentXpath"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("elementXpath"))).click();
    
  • 该元素可能是可点击,但位于加载器后面。在这种情况下,您必须等待加载程序消失,然后才能与所需的元素进行交互,如下所示:

    new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("loader_cssSelector")));
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("element_cssSelector"))).click();
    

0
投票

根据我的经验,硒有时会点击其他地方,我会收到:“另一个元素将在该位置收到点击”;

可能按钮太小或者页面布局很奇怪...

真正解决我的问题的是使用javascript:

((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", 按钮);

我注意到的唯一缺点是它无法处理警报。

© www.soinside.com 2019 - 2024. All rights reserved.