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

PyCharm中Selenium XPATH表达式无效报错求助

Fixing Your Invalid XPath Selector Error in Selenium

Hey Kyle, I spot the issue right away—your XPath expressions are using full-width Chinese single quotes (’) instead of standard half-width English single quotes ('). XPath parsers only recognize English-style quotes, which is why you're getting that InvalidSelectorException.

Let's break down the problem and fix it step by step:

1. The Root Cause

Look at your XPath strings, for example:

//a[@class=’text-bold’]

The quotes around text-bold are full-width (’), not the standard half-width ('). This throws a syntax error because the browser's XPath evaluator can't parse non-English quote characters.

2. Fixing the XPath Expressions

Replace all full-width quotes with half-width quotes in your XPath strings. Also, there's a second issue: you're using find_element (singular) for the language elements but trying to iterate over it like a list—switch that to find_elements (plural) instead.

Here's the corrected code:

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

option = webdriver.ChromeOptions()
# Fix: use half-width dashes for the incognito argument
option.add_argument('--incognito')

browser = webdriver.Chrome(executable_path=r"C:\Users\Kyle Linden\Downloads\chromedriver")
browser.get('https://github.com/TheDancerCodes')

timeout = 20
try:
    WebDriverWait(browser, timeout).until(EC.visibility_of_element_located((By.XPATH, "//img[@class='avatar width-full rounded-2']")))
except TimeoutException:
    print('Timed out waiting for page to load')
    browser.quit()

# Fix: use half-width quotes in XPath
titles_element = browser.find_elements(By.XPATH, "//a[@class='text-bold']")
titles = [x.text for x in titles_element]
print('titles:')
print(titles, '\n')

# Fix: use half-width quotes + switch to find_elements (plural)
language_element = browser.find_elements(By.XPATH, "//p[@class='mb-0 f6 text-gray']")
languages = [x.text for x in language_element]
print('languages:')
print(languages, '\n')

for title, language in zip(titles, languages):
    print("RepoName : Language")
    print(title + ": " + language, '\n')

browser.quit()  # Don't forget to close the browser when done!

3. Bonus Fix: Chrome Options Argument

I also fixed the incognito argument—you had a full-width dash (—) instead of two half-width dashes (--). The original argument wouldn't activate incognito mode, so that's a quick tweak to make sure your browser runs as intended.

4. How to Avoid This in the Future

  • When writing XPath/CSS selectors, always use English keyboard characters for quotes and symbols.
  • Double-check copy-pasted code from tutorials—sometimes formatting can get messed up and introduce non-standard characters.
  • Use your IDE's syntax highlighting: most editors will highlight valid string quotes, so if your quotes look different (like darker or wider), that's a red flag.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:25:36