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

Selenium无法点击表格最后两个带加号按钮的条目问题排查

Selenium自动化点击最后两个加号按钮失败的解决方法

问题描述

我尝试用Selenium自动化操作某统计网站,需要点击“规格”标签下表格里每个分类对应的加号图标,但最后两个分类(XX - Miscellaneous manufactured articles、XXI - Works of art, collectors' pieces and antiques)的加号始终无法点击。

为了避免点击后页面更新导致的Stale Element Exception,我把核心逻辑封装到函数里,确保每次循环都重新获取元素列表,但前面的分类都能正常点击,唯独最后两个不行。

我使用的代码如下:

def fun_trade_products(trade_product):
    trade_products = WebDriverWait(driver, delay).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "di-cl.ewr-ca")))
    for j in trade_products:
        if j.text in trade_product:

            #driver.execute_script("arguments[0].scrollIntoView();", j) 
            print(j.text, "-------------------------")
            plusclick = WebDriverWait(j, delay).until(EC.presence_of_element_located((By.TAG_NAME, "td")))
            sleep(2)
            #driver.execute_script("arguments[0].scrollIntoView();", plusclick) 
            plusclick.click()
            sleep(3)
            return None
product_list = ['I - Live animals; animal products', 'II - Vegetable products', 'III - Fats and oils', 'IV - Prepared foodstuffs', 'V - Mineral products', 'VI - Products of the chemical industry', 'VII - Plastics and rubber and articles thereof', 'VIII - Raw hides and skins, articles thereof', 'IX - Wood and articles of wood', 'X - Pulp of wood, paper, paperboard and articles thereof', 'XI - Textiles and textile articles', 'XII - Footwear, headgear, etc.', 'XIII - Articles of stone, ceramic products, glass', 'XIV - Pearls, precious stones and metals, articles thereof', 'XV - Base metals and articles thereof', 'XVI - Machinery and mechanical appliances, electrical and electrotechnical equipment', 'XVII - Transport equipment', 'XVIII - Optical, photographic, measuring, checking instruments, etc.', 'XIX - Arms and ammunition', 'XX - Miscellaneous manufactured articles', "XXI - Works of art, collectors' pieces and antiques"]

for i in range(len(product_list)):
    fun_trade_products(product_list[i])

解决建议

1. 恢复并优化滚动逻辑

最后两个元素大概率在页面可视区域外,Selenium无法点击不可见元素。取消注释滚动代码,同时调整参数确保元素完全进入可视范围:

driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", j)
# 点击加号前也对目标元素滚动
driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", plusclick)

2. 精准定位加号按钮

当前用By.TAG_NAME, "td"定位容易选中错误单元格,建议根据页面DOM结构直接定位加号元素:

# 示例:通过包含加号的class定位(需根据实际页面调整)
plusclick = WebDriverWait(j, delay).until(EC.element_to_be_clickable((By.CLASS_NAME, "expand-plus")))
# 或通过XPATH匹配图标属性
plusclick = WebDriverWait(j, delay).until(EC.element_to_be_clickable((By.XPATH, ".//*[contains(@class, 'plus-icon')]")))

3. 替换等待条件

将presence_of_element_located改为element_to_be_clickable,确保元素不仅存在,还处于可交互状态:

plusclick = WebDriverWait(j, delay).until(EC.element_to_be_clickable((By.TAG_NAME, "td")))

4. 修正文本匹配逻辑

避免因空格、换行符导致匹配失败,改用精准匹配:

if j.text.strip() == trade_product.strip():

5. 备选:JavaScript强制点击

如果常规点击失效,用JS强制触发点击事件:

driver.execute_script("arguments[0].click();", plusclick)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 04:55:40