首次使用Selenium开发卡牌价格计算器遭遇XPath表达式无效错误求助
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:
- Incorrect attribute matching: You used a comma
,instead of an equals sign=to match theclassattribute. XPath requires@attribute="value"for exact attribute matches. - Wrong
contains()function syntax: Thecontains()function takes two separate arguments (what to check, what to match), not a single equality statement. Socontains(text()="Mewtwo GX")needs to becontains(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 ofcontains(). 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, trynormalize-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
hrefattribute (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

