You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium切换内部Tab时遇ElementNotInteractableException问题求助

解决Selenium切换Tab后ElementNotInteractableException问题

首先明确:不能像切换iframe那样切换div——iframe是独立的浏览器上下文,而这里的Tab只是通过CSS(如display: none或visibility: hidden)控制div的可见性,元素仍存在于当前页面DOM中,只是不可见。

核心问题原因

切换到第二个Tab后,你要定位的元素(比如CurrentPage)在第一个隐藏的div里也有相同类名,Selenium会优先找到那个隐藏的不可交互元素,因此抛出错误。

具体修复步骤

  1. 等待Tab切换完成,确保目标div可见
    不要仅等待Header元素存在,要等待目标Tab对应的div变为可见状态,确认切换完成:

    # 切换Tab后,等待目标Tab的div可见
    target_tab_div = WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located((By.XPATH, "//div[@id='ctl00_cphMaster_ConsultationCitoyenResultatsRecherche_ResultatsRechercheTabContainer_RechercheLETabPanel']"))
    )
    
  2. 限定元素查找范围在可见Tab内
    先定位当前可见的Tab容器,再在容器内部查找元素,避免定位到隐藏div里的同名元素:

    # 先获取当前可见的Tab容器
    visible_tab = WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located((By.XPATH, "//div[contains(@id,'RechercheLETabPanel') and not(contains(@style,'display:none'))]"))
    )
    # 在可见容器内查找列表项
    listings = visible_tab.find_elements(By.CLASS_NAME, "InscriptionSummaryValue")
    # 在可见容器内查找当前页码
    page = int(visible_tab.find_element(By.XPATH, ".//span[@class='CurrentPage']").text)
    
  3. 替换过时API
    代码中find_elements_by_class_name是Selenium 4.x已废弃的方法,建议改用find_elements(By.CLASS_NAME, ...)。

完整修正后的代码片段

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

driver.get(url)
# 输入搜索词
driver.find_element(By.XPATH, "//input[@id='ctl00_cphMaster_ConsultationCitoyenCriteresRecherche_CritereRechercheTabContainer_RechercheSimpleTabPanel_RechercheSimpleControl_MotsClesTextBox']").send_keys(string)
# 选择单选按钮
driver.find_element(By.XPATH, "//input[@id='ctl00_cphMaster_ConsultationCitoyenCriteresRecherche_CritereRechercheTabContainer_RechercheSimpleTabPanel_RechercheSimpleControl_PorteeRadioButtonList_1']").click()
# 点击搜索
driver.find_element(By.XPATH, "//input[@class='Bouton BoutonRechercher']").click()

# 等待搜索结果加载
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,"//div[@class='Header']")))
# 切换到第二个Tab
driver.find_element(By.XPATH, '//*[@id="__tab_ctl00_cphMaster_ConsultationCitoyenResultatsRecherche_ResultatsRechercheTabContainer_RechercheLETabPanel"]').click()

# 等待目标Tab可见
WebDriverWait(driver, 20).until(
    EC.visibility_of_element_located((By.XPATH, "//div[@id='ctl00_cphMaster_ConsultationCitoyenResultatsRecherche_ResultatsRechercheTabContainer_RechercheLETabPanel']"))
)

while True:
    # 获取当前可见的Tab容器
    visible_tab = WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located((By.XPATH, "//div[contains(@id,'RechercheLETabPanel') and not(contains(@style,'display:none'))]"))
    )
    # 在可见容器内获取列表项
    listings = visible_tab.find_elements(By.CLASS_NAME, "InscriptionSummaryValue")
    c = 1
    for i in listings:
        # 在可见容器内获取当前页码
        page = int(visible_tab.find_element(By.XPATH, ".//span[@class='CurrentPage']").text)
        # 此处添加你的业务处理逻辑
        print(f"当前页码:{page},第{c}个列表项")
        c += 1
    # 可在此处添加翻页逻辑,比如定位下一页按钮并点击

内容的提问来源于stack exchange,提问作者DouxDoux

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 04:05:19