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

Selenium按钮点击失效求助:Python代码报InvalidSelectorException错误

解决Selenium点击Donate按钮失败的问题

问题背景

作为Python Selenium初学者,尝试点击https://maskun.org/donate/页面上的捐赠按钮但始终失败,使用的代码如下:

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


chromedriver_location = executable_path=r"C:\Users\HP\Desktop\chromedriver_win32\chromedriver.exe"

driver = webdriver.Chrome(chromedriver_location)
driver.get('https://maskun.org/donate/')

donate_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'give-btn advance-btn')] and text()='Donate Now']")))
donate_button.click()

运行后抛出如下错误:

Traceback (most recent call last):
  File "d:\pythonprojects\maskun.py", line 13, in <module>
    donate_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'give-btn advance-btn')] and text()='Donate Now']")))
  File "C:\Python\Python310\lib\site-packages\selenium\webdriver\support\wait.py", line 81, in until
    value = method(self._driver)
  File "C:\Python\Python310\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 312, in _predicate
    target = driver.find_element(*target)  # grab element at locator
  File "C:\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 856, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 429, in execute
    self.error_handler.check_response(response)
  File "C:\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //button[contains(@class, 'give-btn advance-btn')] and text()='Donate Now'] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//button[contains(@class, 'give-btn advance-btn')] and text()='Donate Now']' is not a valid XPath expression.      
  (Session info: chrome=106.0.5249.119)
Stacktrace:
Backtrace:
        Ordinal0 [0x00B51ED3+2236115]
        Ordinal0 [0x00AE92F1+1807089]
        Ordinal0 [0x009F66FD+812797]
        Ordinal0 [0x009F92B4+823988]
        Ordinal0 [0x009F9165+823653]
        Ordinal0 [0x009F9400+824320]
        Ordinal0 [0x00A25352+1004370]
        Ordinal0 [0x00A257CB+1005515]
        Ordinal0 [0x00A57632+1209906]
        Ordinal0 [0x00A41AD4+1120980]
        Ordinal0 [0x00A559E2+1202658]
        Ordinal0 [0x00A418A6+1120422]
        Ordinal0 [0x00A1A73D+960317]
        Ordinal0 [0x00A1B71F+964383]
        GetHandleVerifier [0x00DFE7E2+2743074]
        GetHandleVerifier [0x00DF08D4+2685972]
        GetHandleVerifier [0x00BE2BAA+532202]
        GetHandleVerifier [0x00BE1990+527568]
        Ordinal0 [0x00AF080C+1837068]
        Ordinal0 [0x00AF4CD8+1854680]
        Ordinal0 [0x00AF4DC5+1854917]
        Ordinal0 [0x00AFED64+1895780]
        BaseThreadInitThunk [0x76046739+25]
        RtlGetFullPathName_UEx [0x77858FD2+1218]
        RtlGetFullPathName_UEx [0x77858F9D+1165]

按钮对应的HTML结构为:

<button class="give-btn advance-btn" tabindex="1">Donate Now<i class="fas fa-chevron-right"></i></button>

错误原因

  1. XPath语法错误:原XPath把多个条件写在了方括号外,且多了一个冗余的闭合],正确写法是所有条件都放在同一个方括号内。
  2. class匹配逻辑错误:contains(@class, 'give-btn advance-btn')会匹配包含完整字符串give-btn advance-btn的class,但实际元素的class是两个独立的类名,这样写容易出问题,应该分开匹配每个类。
  3. 文本匹配问题:按钮内除了Donate Now文本,还有一个<i>标签的图标,直接用text()='Donate Now'会匹配失败,因为text()会返回所有子节点的文本内容,需要用normalize-space()或者contains()来处理。

修正方案

方案1:修正XPath写法

使用normalize-space()处理文本,同时正确匹配class:

donate_button = WebDriverWait(driver, 20).until(
    EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'give-btn') and contains(@class, 'advance-btn') and normalize-space(text())='Donate Now']"))
)

方案2:使用Chrome生成的XPath

直接用浏览器检查得到的XPath定位:

donate_button = WebDriverWait(driver, 20).until(
    EC.element_to_be_clickable((By.XPATH, "//*[@id='give-form-746-1']/div[1]/button"))
)

方案3:使用CSS选择器(推荐)

CSS选择器定位更简洁可靠:

donate_button = WebDriverWait(driver, 20).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, "button.give-btn.advance-btn"))
)

完整修正代码

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

chromedriver_location = executable_path=r"C:\Users\HP\Desktop\chromedriver_win32\chromedriver.exe"

driver = webdriver.Chrome(chromedriver_location)
driver.get('https://maskun.org/donate/')

# 选用任意一种方案即可,这里以CSS选择器为例
donate_button = WebDriverWait(driver, 20).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, "button.give-btn.advance-btn"))
)
donate_button.click()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 09:50:26