【问题标题】:Python webscraping with Selenium chrome driver使用 Selenium chromedriver 进行 Python 网页抓取
【发布时间】:2020-03-21 18:34:06
【问题描述】:

我正在尝试通过使用 Python SeleniumChrome 驱动程序 来获取跨度标记中的 Instagram 帐户的发布数量,这是html代码:

<!doctype html>
<html lang="fr" class="js logged-in client-root js-focus-visible sDN5V">
<head>-</head>
  <body class style>
    <div id="react-root"> == 50
    <form enctype^murtipart/form-data" method="POST" role="presentation">_</form>    
    <section class=”_9eogI E3X2T">
     <div></div>
     <main class="SCxLW o64aR " role=”main">
      <div class=”v9tJq AAaSh VfzDr">
        <header class=" HVbuG">_</header>
►       <div class="-vDIg">_</div>
►       <div class="_4bSq7">_</div>
▼       <ul class=” _3dEHb">
▼         <li class=” LH36I">
▼           <span class=" _81NM2">
                <span class="g47SY 10XF2">6 588</span>
                "publications"
            </span>
          </li>

Python 代码

def get_publications_number(self, user):
    self.nav_user(user)
    sleep(16)
    publication = self.driver.find_element_by_xpath('//div[contains(id,"react-root")]/section/main/div/ul/li[1]/span/span')

错误信息

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
 {"method":"xpath","selector":"//div[contains(id,"react-root")]/section/main/div/ul/li[1]/span/span"}
  (Session info: chrome=80.0.3987.149)

重要提示:

这个 xpath 是从 Chrome 元素检查器粘贴的,所以我认为不是问题所在。当我输入self.driver.find_elements_by_xpath()(带有's')时不会出现错误,如果我这样做:

for value in publication: print(value.text)

也不会有错误,但不会打印任何内容

所以问题是:

为什么我在 Xpath 存在时收到此错误?

【问题讨论】:

  • 你做过调试吗?如何从 XPath 中逐个删除元素并每次检查结果?另外,'//div[contains(id,"react-root")]' 不应该只是'//div[@id="react-root")]' 吗?
  • 我想过,但是这个路径是由 Chrome 的元素检查器给出的,所以路径实际上不是问题

标签: python python-3.x selenium web-scraping selenium-chromedriver


【解决方案1】:

试试

'//div[@id="react-root"]//ul/li//span[contains(., "publications")]/span'

说明:

  • //div[@id="react-root"]

  • //ul/li //),它们是 li 元素,它们是 ul 标记元素的子元素

  • //span[contains(., "publications")] li 元素中查找包含publications 作为文本的任何span 元素
  • /span获取找到的span的span元素

还有一点:find_element_by_xpath 返回第一个匹配的元素。如果您有多个“出版物”,如果您在 selenium 中仅使用 find_elements_by_xpath 而不是 find_element_by_xpath,则可以使用上面的 xpath(如果您愿意)收集它们。

最近我发现this page 是一本很好的开始掌握 Xpath 的读物,如果您想了解更多信息,请查看它。

【讨论】:

  • 不起作用:selenium.common.exceptions.InvalidSelectorException:消息:无效选择器:无法使用 xpath 表达式定位元素 //div[@id="react-root")]/ /span/span[contains(text(), "publications"] 因为以下错误: SyntaxError: 无法在 'Document' 上执行 'evaluate': The string '//div[@id="react-root") ]//span/span[contains(text(), "publications"]' 不是有效的 XPath 表达式。
  • @Seye 有一个错字,已修复
  • 一个错字,xpath 是正确的,所以没有错字
  • 我已经更新了我的答案,而不是你的问题(错字)
  • @Seye 添加了我的最终编辑,它通过查找文本节点“publications”来查找您的跨度,然后选择容器跨度的子跨度。
【解决方案2】:
//div[contains(id,"react-root")]/section/main/div/ul/li[1]/span/span

使用这个 Xpath。它可能会起作用。我认为你在那里犯了一个昏迷错误。

【讨论】:

  • 我没有忘记“id”后面的昏迷
  • 您还尝试复制粘贴吗?我必须消除额外的昏迷,这就是我提出它的原因。
  • 尝试进入页面,点击元素,点击'Copy full Xpath'然后粘贴。
  • 这正是我所做的
猜你喜欢
  • 2020-07-02
  • 2021-05-08
  • 2018-07-20
  • 2020-03-13
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多