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

使用Selenium无法从PRO LISTS下拉列表选择CS:GO选项的问题求助

定位失败问题解决方案

定位失败核心是两个问题:一是文本匹配大小写不匹配,二是依赖了动态生成的class属性导致定位不稳定。调整后的逻辑如下:

  • 修正PRO LISTS文本匹配规则,使用精准全匹配避免大小写问题导致的元素找不到
  • 移除对x-active这类hover后才生成的动态class的依赖,直接通过菜单元素的固有属性定位
  • 改用JS点击兜底,避免元素被遮挡导致的普通点击失效问题
  • 增加表格加载等待逻辑,确保后续数据提取时页面已经渲染完成

修正后的代码如下:

import scrapy
from scrapy.selector import Selector
from scrapy_selenium import SeleniumRequest
from selenium.webdriver.common.keys import Keys
import time

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


class DropdownScrollTable(scrapy.Spider):
    name = 'dst'

    def start_requests(self):
        yield SeleniumRequest(
            url='https://prosettings.net/cs-go-pro-settings-gear-list/',
            wait_time=5,
            screenshot=True,
            callback=self.parse)

    def parse(self, response):
        driver = response.meta['driver']
        wait = WebDriverWait(driver, 20)
        # 定位PRO LISTS导航项并触发hover
        pro_lists_nav = wait.until(EC.visibility_of_element_located((By.XPATH, "//span[text()='PRO LISTS']/ancestor::a")))
        ActionChains(driver).move_to_element(pro_lists_nav).pause(2).perform()
        # 定位CS:GO选项并点击
        csgo_option = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='CS:GO']/ancestor::a[contains(@class,'x-anchor-menu-item')]")))
        driver.execute_script("arguments[0].click();", csgo_option)
        # 等待表格加载完成
        wait.until(EC.visibility_of_element_located((By.ID, "pro-player-table")))
        # 保存截图验证结果
        driver.save_screenshot('search_result.png')
        # 后续可通过driver.page_source获取页面源码,用Selector解析表格数据

运行后查看search_result.png即可看到CS:GO职业选手设置表格已经正常加载,后续可正常提取所需数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 20:12:01