selenium - 字符串不是有效的xpath表达式

时间:2018-05-18 00:28:36

标签: django selenium xpath pycharm

我在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 text-grey']无效。

1 个答案:

答案 0 :(得分:2)

不幸的是,您从中获取示例的教程网站已被一些过于聪明的文字处理软件所破坏,因此ASCII打字机引号('")已被转换为印刷报价(“......”),('...')。 XPath需要ASCII打字机种类。