我正在PyCharm中运行一个selenium教程,并得到一个无效的XPATH表达式。我已经查看了Selenium文档,似乎我正在正确地编写XPATH。它打开Chrome刚刚好,一定是看到图像化身后加载。然后,它将获得XPATH错误。
我正在尝试执行以下教程:https://medium.com/the-andela-way/introduction-to-web-scraping-using-selenium-7ec377a8cf72
selenium.common.exceptions.InvalidSelectorException: Message: invalid
selector: Unable to locate an element with the xpath expression //a[@class
=’text-bold’] because of the following error: SyntaxError: Failed to execute
'evaluate' on 'Document': The string '//a[@class=’text-bold’]' is not a
valid XPath expression. (Session info: chrome=66.0.3359.181) (Driver
info: chromedriver=2.38.552522
(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT
10.0.16299 x86_64)这是我的密码:
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
# https://medium.com/the-andela-way/introduction-to-web-scraping-using-selenium-7ec377a8cf72
option = webdriver.ChromeOptions()
option.add_argument(' — incognito')
# Now create an 'instance' of your driver
# This path should be to wherever you downloaded the driver
browser = webdriver.Chrome(executable_path=r"C:\Users\Kyle Linden\Downloads\chromedriver")
# A new Chrome (or other browser) window should open up
browser.get('https://github.com/TheDancerCodes')
# Wait 20 seconds for page to load
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()
# find_elements_by_xpath returns an array of selenium objects.
titles_element = browser.find_elements(By.XPATH, "//a[@class=’text-bold’]")
# use list comprehension to get the actual repo titles and not the selenium objects.
titles = [x.text for x in titles_element]
# print out all the titles.
print('titles:')
print(titles, '\n')
language_element = browser.find_element(By.XPATH, "//p[@class=’mb-0 f6 text-gray’]")
# same concept as for list-comprehension above.
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')我不明白为什么//p@class=‘mb-0 f6文本-灰色’无效。
发布于 2018-05-18 07:29:56
不幸的是,您正在学习的教程站点已经被一些过于聪明的字处理软件破坏了,从而使ASCII打字机引号('和")变成了排版引号(“…”)。,(“…”)。XPath需要ASCII打字机的品种。
https://stackoverflow.com/questions/50402181
复制相似问题