无法单击元素:Splinter/Selenium 中的 ElementClickInterceptedException

2025-01-15 08:45:00
admin
原创
158
摘要:问题描述:我正在尝试抓取一个页面,但有时无法单击链接/按钮。当网页加载时,会首先出现“loadingWhiteBox”,然后在几秒钟后消失(但它会保留在 HTML 代码中),只要该框出现在网站上,我就无法点击链接并收到以下错误消息:selenium.common.exceptions.ElementClick...

问题描述:

我正在尝试抓取一个页面,但有时无法单击链接/按钮。

当网页加载时,会首先出现“loadingWhiteBox”,然后在几秒钟后消失(但它会保留在 HTML 代码中),只要该框出现在网站上,我就无法点击链接并收到以下错误消息:

selenium.common.exceptions.ElementClickInterceptedException: Message: 
Element <span class="taLnk ulBlueLinks"> is not clickable at point 
(318.3000030517578,661.7999877929688) because another element <div 
class="loadingWhiteBox"> obscures it

有什么办法可以解决这个问题吗?我已经尝试使用以下命令:

driver.is_element_present_by_css('div[class*="loadingWhiteBox"]')

但即使不活跃,该元素仍然存在。


解决方案 1:

您可以尝试以下两种方法来单击元素。

element = driver.find_element_by_css('div[class*="loadingWhiteBox"]')
driver.execute_script("arguments[0].click();", element)

element = driver.find_element_by_css('div[class*="loadingWhiteBox"]')
webdriver.ActionChains(driver).move_to_element(element ).click(element ).perform()

希望这会起作用。

解决方案 2:

此错误信息...

selenium.common.exceptions.ElementClickInterceptedException: Message: Element <span class="taLnk ulBlueLinks"> is not clickable at point (318.3000030517578,661.7999877929688) because another element <div class="loadingWhiteBox"> obscures it

...意味着所需元素不可点击,因为其他元素遮挡了它。


有多种方法可以解决这个问题,其中几种如下:

  • 当您打算调用时,click()您需要结合WebDriverWait和WebDriverWait来调用WebDriverWait,element_to_be_clickable()并且您可以使用以下任一定位器策略:

+ 使用`CSS_SELECTOR`:


WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.taLnk.ulBlueLinks"))).click()
+ 使用`XPATH`:


WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='taLnk ulBlueLinks']"))).click()
  • 如果错误...另一个元素掩盖了它...仍然存在,首先您需要结合阻塞元素来诱导 WebDriverWait,如下所示expected_conditions`invisibility_of_element()`

+ 使用`CSS_SELECTOR`:


WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.CSS_SELECTOR, "div.loadingWhiteBox")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.taLnk.ulBlueLinks"))).click()
+ 使用`XPATH`:


WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.XPATH, "//div[@class='loadingWhiteBox']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='taLnk ulBlueLinks']"))).click()
  • 如果问题依然存在,您可以使用execute_script()如下方法:

+ 使用`CSS_SELECTOR`:


WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.CSS_SELECTOR, "div.loadingWhiteBox")))
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.taLnk.ulBlueLinks"))))
+ 使用`XPATH`:


WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.XPATH, "//div[@class='loadingWhiteBox']")))
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='taLnk ulBlueLinks']"))))

笔记

您必须添加以下导入:

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

解决方案 3:

你可以等到元素消失,

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("loadingWhiteBox")));

解决方案 4:

对于硒化物

WebElement element = selenide_element.toWebElement();
WebDriverRunner.driver().executeJavaScript("arguments[0].click();", element);

解决方案 5:

当我收到此错误时,我通常会尝试不同的方法。而不是:

driver.findElement(By.cssSelector("div[class*="loadingWhiteBox"]")).click();

尝试一下:

WebElement webElement = driver.findElement(By.cssSelector("div[class*="loadingWhiteBox"]"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", webElement);

即使有覆盖,这也将单击找到的 webElement。

如果这不起作用,请确保您尝试单击正确的“可单击”Web 元素,并检查您的 css 选择器是否指向不同的 webElement。我所说的“可单击”是指单击时执行操作的 webElement(例如打开新页面)。Web 驱动程序将单击它,您可能认为它实际上没有执行单击操作,但实际上它在错误的 webElement 上执行了该操作。

解决方案 6:

我遇到了同样的问题,我只是用了这个:

elm = driver.find_elements_by_css_selector('div[class*="loadingWhiteBox"]')
elm.click()

解决方案 7:

以下是删除最多 10 个拦截元素的通用方法:

public void clickAndDeleteInterceptingElement(By selector) {
    int numberOfTries = 3;
    boolean isExceptionExisting = true;
    while (numberOfTries > 0 && isExceptionExisting) {
        numberOfTries--;
        try {
            clickElement(findElement(selector));
            isExceptionExisting = false;
        } catch (ElementClickInterceptedException e) {
            String element = e.getMessage().split("Other element would receive the click:")[1];
            element = element.split(">")[0];
            element = element.replace(" <", "");
            String tag = element.split(" ")[0];
            String attributes = "[" + element.replace(tag + " ", "") + "]";
            String resultString = "";
            boolean isInsideAttributeValue = false;
            boolean areInvertedCommasOpeningOnes = true;
            for (int i = 0; i < attributes.length(); i++) {
                char c = attributes.charAt(i);
                if (c == '"' && areInvertedCommasOpeningOnes) {
                    isInsideAttributeValue = true;
                    areInvertedCommasOpeningOnes = false;
                } else if (c == '"' && !areInvertedCommasOpeningOnes) {
                    isInsideAttributeValue = false;
                    areInvertedCommasOpeningOnes = true;
                }
                if (c == ' ' && isInsideAttributeValue) {
                    resultString += "spaceInsideAttributeValue";
                } else {
                    resultString += c;
                }
            }
            resultString = resultString.replace(" ", "][");
            resultString = resultString.replace("spaceInsideAttributeValue", " ");
            String cssSelectorString = tag + resultString;
            try {
                deleteElement(By.cssSelector(cssSelectorString));
            } catch (WebDriverException e2) {
                e.printStackTrace();
                e2.printStackTrace();
                break;
            }
            sleep(1000);
        }
    }
    if (isExceptionExisting) {
        clickElement(findElement(selector));
    }
}
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   3975  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   2742  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Freshdesk、ClickUp、nTask、Hubstaff、Plutio、Productive、Targa、Bonsai、Wrike。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在项目管理过程中面临着诸多痛点,如任务分配不...
项目管理系统   80  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Monday、TeamGantt、Filestage、Chanty、Visor、Smartsheet、Productive、Quire、Planview。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多项目经理和团队在管理复杂项目时,常...
开源项目管理工具   88  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Smartsheet、GanttPRO、Backlog、Visor、ResourceGuru、Productive、Xebrio、Hive、Quire。在当今快节奏的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在选择项目管理工具时常常面临困惑:...
项目管理系统   77  
热门文章
项目管理软件有哪些?
曾咪二维码

扫码咨询,免费领取项目管理大礼包!

云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用