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

如何用Playwright点击展开分类树所有节点?代码修复求助

修复Playwright无法完全展开分类树所有子节点的问题

问题描述

我想用Playwright自动点击并展开分类树的所有子节点,但现有代码只能展开部分节点:

  • 当前效果:仅部分分类节点被展开,仍有未展开的子节点
  • 预期效果:所有层级的分类节点全部展开

现有代码如下:

import json
import time
from playwright.sync_api import sync_playwright

p = sync_playwright().start()

browser = p.chromium.launch(headless=False, slow_mo=2000)
context = browser.new_context()
page = context.new_page()  

try:
    # page.add_init_script(js);
    page.goto("https://keepa.com/#!categorytree", timeout=10000)

    # Click text=Log in / register now to subscribe
    page.click("text=Log in / register now to subscribe")
    # Click input[name="username"]
    page.click("input[name=\"username\"]")
    # Fill input[name="username"]
    page.fill("input[name=\"username\"]", "tylrr123@outlook.com")
    # Click input[name="password"]
    page.click("input[name=\"password\"]")
    # Fill input[name="password"]
    page.fill("input[name=\"password\"]", "adnCgL@f$krY9Q9")
    # Click input:has-text("Log in")
    page.click("input:has-text(\"Log in\")")
    page.wait_for_timeout(2000)
    page.goto("https://keepa.com/#!categorytree", timeout=10000)

    while(True):
        #loc.first.click()
        loc = page.locator(".ag-icon.ag-icon-expanded")
        print(loc.count())
        loc.first.click(timeout=5000)
        page.wait_for_timeout(2000)


except Exception as err:
    print(err)
finally:
    print("finished")

问题原因

你的代码每次只点击第一个未展开节点,但展开父节点后,新出现的子节点可能排在当前节点之前,导致后续循环重复点击已处理的节点,遗漏深层节点。另外,固定等待时间wait_for_timeout不够可靠,页面未完全渲染就继续循环会导致节点识别失败。

修复后的代码

import json
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False, slow_mo=1000)
    context = browser.new_context()
    page = context.new_page()  

    try:
        page.goto("https://keepa.com/#!categorytree", timeout=10000)

        # 登录流程
        page.click("text=Log in / register now to subscribe")
        page.fill("input[name=\"username\"]", "tylrr123@outlook.com")
        page.fill("input[name=\"password\"]", "adnCgL@f$krY9Q9")
        page.click("input:has-text(\"Log in\")")
        page.wait_for_load_state("networkidle")  # 等待页面加载完成,替代固定等待
        page.goto("https://keepa.com/#!categorytree", timeout=10000)
        page.wait_for_load_state("networkidle")

        while True:
            # 获取当前所有可展开的节点列表
            expandable_nodes = page.locator(".ag-icon.ag-icon-expanded").all()
            if not expandable_nodes:
                break  # 无节点可展开时退出循环
            
            # 逐个点击当前可展开节点
            for node in expandable_nodes:
                node.scroll_into_view_if_needed()  # 确保节点在视口中可见
                node.click()
                page.wait_for_timeout(500)  # 等待节点展开后的状态更新,可按需调整时长

    except Exception as err:
        print(err)
    finally:
        browser.close()
        print("finished")

关键修复说明

  • 遍历所有可展开节点:每次循环获取当前页面所有未展开节点并逐个点击,避免只处理第一个节点导致的遗漏
  • 优化等待逻辑:用page.wait_for_load_state("networkidle")替代固定等待,确保页面加载完成后再操作;点击后用短时间等待确保节点状态更新
  • 保证节点可见:点击前调用scroll_into_view_if_needed(),避免因节点不在视口内导致点击失败
  • 明确退出条件:当没有可展开节点时退出循环,避免无限循环报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 18:21:27