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

首次使用Selenium开发卡牌价格计算器遭遇XPath表达式无效错误求助

Fixing the Invalid XPath Error in Your Selenium Card Price Calculator

Hey Jacopo, let's get that XPath issue sorted out for your card price tool! The error is pointing to two clear syntax mistakes in your XPath expression—here's how to fix them, plus some extra tips to avoid similar headaches later.

The Two Broken Parts of Your XPath

Let's break down why //div[@class, "col-12 col-md-8 px-2 flex-column"]/a[contains(text()="Mewtwo GX")] is invalid:

  1. Incorrect attribute matching: You used a comma , instead of an equals sign = to match the class attribute. XPath requires @attribute="value" for exact attribute matches.
  2. Wrong contains() function syntax: The contains() function takes two separate arguments (what to check, what to match), not a single equality statement. So contains(text()="Mewtwo GX") needs to be contains(text(), "Mewtwo GX").

Corrected XPath & Code

Replace your problematic line with this fixed version:

driver.find_element(By.XPATH, '//div[@class="col-12 col-md-8 px-2 flex-column"]/a[contains(text(), "'+product+'")]').click()

Pro Tips to Avoid Future Issues

1. Wait for Elements to Load

Pages don't load instantly, and trying to click an element before it's ready will cause more errors. Use WebDriverWait to wait for the element to be clickable:

# Add these imports at the top of your file
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# After clicking the search button
wait = WebDriverWait(driver, 10)  # Wait up to 10 seconds
card_link = wait.until(EC.element_to_be_clickable((By.XPATH, '//div[@class="col-12 col-md-8 px-2 flex-column"]/a[contains(text(), "'+product+'")]')))
card_link.click()

2. Handle Text Matching Carefully

  • If you need an exact text match (not just containing the text), use text()="Your Exact Card Name" instead of contains(). Just make sure to account for any spaces or capitalization differences.
  • Sometimes card names might have extra whitespace or be split across child elements—if text() doesn't work, try normalize-space(text()) to clean up whitespace:
    //div[@class="col-12 col-md-8 px-2 flex-column"]/a[contains(normalize-space(text()), "'+product+'")]
    

3. Use More Stable Locators

Class names can change if the website updates its design. If you run into issues later, try using more stable identifiers like:

  • The link's href attribute (if it has a unique part)
  • Parent elements with unique IDs or data attributes

Full Corrected Code Snippet (Relevant Section)

Here's how the end of your code will look with the fixes and recommended wait:

# ... (your existing code before this point) ...

product = product+" "+rarity
driver.find_element(By.ID, "ProductSearchInput").send_keys(product)
driver.find_element(By.ID, "search-btn").click()

# Import wait utilities (add these at the top of your file if you haven't)
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Wait for the card link to be clickable, then click it
wait = WebDriverWait(driver, 10)
card_link = wait.until(EC.element_to_be_clickable((By.XPATH, '//div[@class="col-12 col-md-8 px-2 flex-column"]/a[contains(text(), "'+product+'")]')))
card_link.click()

input()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 19:42:43