python - 无效选择器错误 : Webscraping different kinds of text from multiple spans using xpath and Selenium

标签 python selenium xpath

我正在尝试按以下格式刮出以逗号分隔并带有星号的作者列表[重要]:

名末、名末、名末*、名末

我正在抓取的 html 部分非常复杂,但我已经成功测试了一个 xpath,它会产生我想要的文本和符号。

//span[@class="hlFld-ContribAuthor"]/span[@class="hlFld-ContribAuthor"]/a/text() | //span[@class="NLM_x"]/x/text() | //a[@class="ref"]/sup/text()

结果如下: enter image description here

但是,当我在 python 代码中使用该公式时,出现错误。

我的代码:

# get authors
xpath = "//span[@class=\"hlFld-ContribAuthor\"]/span[@class=\"hlFld-ContribAuthor\"]/a/text() | //span[@class=\"NLM_x\"]/x/text() | //a[@class=\"ref\"]/sup/text()"
authors = driver.find_element_by_xpath(xpath)
print str(authors)

错误:

InvalidSelectorException: Message: The given selector //span[@class="hlFld-ContribAuthor"]/span[@class="hlFld-ContribAuthor"]/a/text() | //span[@class="NLM_x"]/x/text() | //a[@class="ref"]/sup/text() is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: The result of the xpath expression "//span[@class="hlFld-ContribAuthor"]/span[@class="hlFld-ContribAuthor"]/a/text() | //span[@class="NLM_x"]/x/text() | //a[@class="ref"]/sup/text()" is: [object Text]. It should be an element.

如何让 selenium 以正确的顺序获取我需要的正确文本和符号?我无法在没有换行的情况下打印 xpath 的结果。

编辑:通过从 xpath 中删除/text() 解决了 xpath 错误

最佳答案

函数driver.find_element_by_xpath(my_xpath)期望在找到由my_xpath标识的节点时找到一个DOM元素。如果没有,则会抛出错误。您的 XPath 表达式都返回文本节点,因此会导致错误。

要返回 DOM 元素,请将 XPath 表达式更改为:

"//span[@class=\"hlFld-ContribAuthor\"]/span[@class=\"hlFld-ContribAuthor\"]/a |//span[@class=\"NLM_x\"]/x |//a[@class=\"ref\"]/sup"

此外,由于您要返回多个元素,因此应使用 driver.find_elements_by_xpath (注意复数)而不是 driver.find_element_by_xpath

然后,您将能够通过循环 authors 从每个作者元素中获取所需的文本:

for author in authors:
    print(author.text)

关于python - 无效选择器错误 : Webscraping different kinds of text from multiple spans using xpath and Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35491159/

相关文章:

java - 如何获得 "For"操作的结果?

python - 在 PayPal REST API 中,如何从新执行的账单协议(protocol)中获取付款人信息?

javascript - Electron 测试 - WebdriverIO 在 devtools 窗口和主应用程序窗口之间切换

html - 找不到 Chrome DevTools 给出的 XPath

python - 未知错误: chrome failed to start

javascript - JavascriptExecutor 在 selenium webdriver 中是如何工作的

php - 使用PHP从YouTube抓取统计信息

python - 在 Keras 中为模型编译指定多个损失函数

python - python中的低通线性滤波器

python - Python 中带有条件的列表生成器