修复ElementClickInterceptedException并定向抓取指定分类展商信息
问题:修复ElementClickInterceptedException并抓取指定分类展商信息
问题说明
需要从https://asiatechxsg.com/exhibitors/页面筛选"Innovfest x Elevating Founders (Startup/Scaleup)"分类,抓取该分类下所有展商的名称和展位号,但点击筛选下拉框时触发ElementClickInterceptedException错误。
报错信息
ElementClickInterceptedException Traceback (most recent call last) Cell In[6], line 36 34 # Locate and click the dropdown to reveal the checkboxes 35 dropdown = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@data-hook='filter-dropdown']"))) ---> 36 dropdown.click() 37 time.sleep(2) # Allow the dropdown to open 39 # Get all the filter checkboxes File c:\Users\twbsguser009\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webelement.py:94, in WebElement.click(self) 92 def click(self) -> None: 93 """Clicks the element.""" ---> 94 self._execute(Command.CLICK_ELEMENT) File c:\Users\twbsguser009\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webelement.py:395, in WebElement._execute(self, command, params) 393 params = {} 394 params["id"] = self._id ---> 395 return self._parent.execute(command, params) File c:\Users\twbsguser009\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py:347, in WebDriver.execute(self, driver_command, params) 345 response = self.command_executor.execute(driver_command, params) 346 if response: ---> 347 self.error_handler.check_response(response) 348 response["value"] = self._unwrap_value(response.get("value", None)) 349 return response ... (No symbol) [0x00007FF7A3483592] (No symbol) [0x00007FF7A3472F9F] BaseThreadInitThunk [0x00007FFF26B1257D+29] RtlUserThreadStart [0x00007FFF27FEAA48+40]
原代码
# Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger() # Initialize WebDriver driver = webdriver.Chrome() driver.maximize_window() driver.get("https://asiatechxsg.com/exhibitors/") # Wait for the iframe to load and switch to it wait = WebDriverWait(driver, 10) wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, "IframeModule_iframe__JCvXg"))) # Scroll to the bottom of the page to ensure all elements are loaded driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(2) # Allow some time for the page to load new content # Scroll until the last specific element is found match = False while not match: driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(2) # Allow some time for the page to load new content last_element = driver.find_elements(By.XPATH, "//img[@alt='Singapore Centre for Social Enterprise, raiSE Ltd']") if len(last_element) >= 1: match = True # Locate and click the dropdown to reveal the checkboxes dropdown = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@data-hook='filter-dropdown']"))) dropdown.click() time.sleep(2) # Allow the dropdown to open # Get all the filter checkboxes checkboxes = driver.find_elements(By.XPATH, "//input[@type='checkbox' and contains(@data-hook, 'exhibitor-filter')]") exhibitor_names = [] # Loop through each checkbox to apply the filter and gather names for checkbox in checkboxes: # Check the checkbox checkbox.click() time.sleep(2) # Allow some time for the filter to apply # Get the filtered company names filtered_names = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//span[@class='sc-a13c392f-0 sc-85df61db-4 feepZZ eErKol']"))) logger.info(f"No. of sponsors and exhibitors after applying filter= {len(filtered_names)}") # Append the names to the list for name in filtered_names: exhibitor_names.append(name.text) # Uncheck the checkbox to reset the filter checkbox.click() time.sleep(2) # Allow some time for the filter to reset # Close the WebDriver driver.quit() # Print or log all collected exhibitor names logger.info(f"Total exhibitors collected: {len(exhibitor_names)}") for name in exhibitor_names: print(name)
修复方案与修改后的代码
错误原因
ElementClickInterceptedException通常是因为目标元素被其他UI元素(如弹窗、固定导航栏、加载遮罩)遮挡,导致Selenium无法直接点击。
修复要点
- 改用JavaScript点击替代原生
click()方法,绕过元素遮挡问题 - 直接定位目标分类的复选框,无需遍历所有选项,提升效率
- 新增展位号抓取逻辑,完善数据获取
- 用显式等待替代固定
time.sleep(),优化页面加载等待逻辑
修改后的完整代码
import logging from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 配置日志 logging.basicConfig(level=logging.INFO) logger = logging.getLogger() # 初始化浏览器 driver = webdriver.Chrome() driver.maximize_window() driver.get("https://asiatechxsg.com/exhibitors/") try: # 等待iframe加载并切换上下文 wait = WebDriverWait(driver, 15) wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, "IframeModule_iframe__JCvXg"))) # 滚动到筛选区域,避免被导航栏遮挡 filter_section = wait.until(EC.presence_of_element_located((By.XPATH, "//div[@data-hook='filter-dropdown']"))) driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", filter_section) # 用JavaScript点击下拉框,绕过遮挡问题 driver.execute_script("arguments[0].click();", filter_section) logger.info("筛选下拉框已打开") # 等待目标分类复选框加载并点击 target_checkbox = wait.until(EC.element_to_be_clickable( (By.XPATH, "//label[contains(text(), 'Innovfest x Elevating Founders (Startup/Scaleup)')]/preceding-sibling::input[@type='checkbox']") )) driver.execute_script("arguments[0].click();", target_checkbox) logger.info("已选中目标分类") # 等待筛选结果加载完成 wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//div[contains(@class, 'sc-85df61db-3')]"))) # 抓取展商名称和展位号 exhibitor_items = driver.find_elements(By.XPATH, "//div[contains(@class, 'sc-85df61db-3')]") exhibitor_data = [] for item in exhibitor_items: # 获取名称 name_element = item.find_element(By.XPATH, ".//span[@class='sc-a13c392f-0 sc-85df61db-4 feepZZ eErKol']") name = name_element.text.strip() # 获取展位号(处理可能不存在的情况) booth_element = item.find_elements(By.XPATH, ".//span[@class='sc-a13c392f-0 sc-85df61db-5 jfVtqk bnYUXO']") booth_number = booth_element[0].text.strip() if booth_element else "无展位号" exhibitor_data.append({"名称": name, "展位号": booth_number}) # 输出结果 logger.info(f"共抓取到{len(exhibitor_data)}家目标分类展商") for idx, data in enumerate(exhibitor_data, 1): print(f"{idx}. 名称: {data['名称']} | 展位号: {data['展位号']}") finally: # 关闭浏览器 driver.quit()
内容的提问来源于stack exchange,提问作者user22279494
相关产品推荐
相关产品推荐

