PyCharm中Selenium XPATH表达式无效报错求助
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

