无法单击元素:Splinter/Selenium 中的 ElementClickInterceptedException
- 2025-01-15 08:45:00
- admin 原创
- 100
问题描述:
我正在尝试抓取一个页面,但有时无法单击链接/按钮。
当网页加载时,会首先出现“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));
}
}