用selenium爬數(shù)據(jù)的時候,明明每一步點擊都加了WebDriverWait,但還是爬一會兒就顯示如下錯誤:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <tr class="even" onclick="onclick_shipinsp(this,'insp')">...</tr> is not clickable at point (509, 404). Other element would receive the click: <div class="blockUI blockOverlay ui-widget-overlay" style="z-index: 1000; position: fixed; opacity: 0;"></div>
(Session info: chrome=110.0.5481.178)
這里一定要注意后面的這句
“ Other element would receive the click: <div class="blockUI blockOverlay ui-widget-overlay" style="z-index: 1000; position: fixed; opacity: 0;"></div>
(Session info: chrome=110.0.5481.178)
”
也就是說我們想點擊的按鈕沒點成,而是被這個覆蓋了。
查資料得知:
blockUI 是一個 JavaScript 庫,用于創(chuàng)建可定制的頁面遮罩和加載指示器。它提供了一個簡單的方式來阻止用戶在頁面加載或執(zhí)行某些任務時進行交互,并顯示一個加載指示器,以提示用戶頁面正在處理。
解決嘗試一:
加一個異常處理塊,這里try塊中是之前報錯的地方,一旦出現(xiàn)異常則對之前bug說明里出現(xiàn)的元素進行隱藏,然后再次嘗試點擊。
try:
driver.find_element(By.XPATH,xpath_pattern).click()
except:
wait = WebDriverWait(driver, 100)
element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'blockUI blockOverlay ui-widget-overlay')))
driver.execute_script("arguments[0].style.display = 'none';", element)
try:
driver.find_element(By.XPATH,xpath_pattern).click()
嘗試之后:比之前多撐了一會兒但在except塊中還是會報錯找不到元素。
解決嘗試二:文章來源:http://www.zghlxwxcb.cn/news/detail-511590.html
for i in range(3):
try:
click_button(driver, index_xpath)
break
except:
# 如果點擊元素失敗,檢查元素是否存在且可見
block_overlay = driver.execute_script(
"return document.querySelector('.blockUI.blockOverlay.ui-widget-overlay')")
if block_overlay and block_overlay.is_displayed():
# 如果元素存在且可見,隱藏元素
driver.execute_script(
"document.querySelector('.blockUI.blockOverlay.ui-widget-overlay').style.display='none';")
try:
click_button(driver, index_xpath)
break
except:
# 如果還是失敗,等待一段時間再嘗試
time.sleep(10)
目前這種解決方法暫時可行。學習爬蟲不久,有更好方法的朋友歡迎指教。文章來源地址http://www.zghlxwxcb.cn/news/detail-511590.html
到了這里,關于解決selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!