【问题标题】:Selenium Python: Finding span nested in div with a certain css attributeSelenium Python:查找嵌套在具有特定css属性的div中的跨度
【发布时间】:2021-06-01 15:28:30
【问题描述】:

我正在尝试为公共记录构建网络抓取工具。目前,我的 HTML 有几个带有属性信息的“卡片”,包括地址和作品集编号。每张卡片都有以下 HTML。

<div class="results_record ng-scope" ng-repeat="candidate in candidatesList.candidates | orderBy:['siteAddress']">
        
        <span class="record_number ng-binding">1</span>
        
        
        <div class="record_folio ng-binding"><strong>FOLIO:</strong>           
            <span ng-click="getCandidateFolio(candidate.folio)" class="ng-binding">01-4138-159-0001</span> (Reference)
        </div>
        

    </div><!-- end ngRepeat: candidate in candidatesList.candidates | orderBy:['siteAddress'] --><div class="results_record ng-scope" ng-repeat="candidate in candidatesList.candidates | orderBy:['siteAddress']">
        

我想为每张卡片选择作品集编号文本(即本示例中的 01-4138-159-0001)。我的代码如下,使用 Python 的 Selenium,但我没有得到任何结果:

folios =  driver.find_elements_by_xpath('//span[@ng-click="getCandidateFolio(candidate.folio"]')
for folio in folios:
print(folio.text)

有没有更好的选择对开编号的方法?

【问题讨论】:

  • 您是否收到任何错误消息?此外,您还没有在 Candidate.folio 之后关闭括号。
  • @RhysFlook 谢谢,关闭括号后,我现在得到:selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@ngclick="getCandidateFolio(candidate.folio)"]"}

标签: python xml selenium xpath


【解决方案1】:
folios =  driver.find_elements_by_xpath('//span[@ng-click="getCandidateFolio(candidate.folio)"]')

for folio in folios:
      print(folio.text)

有一个 ) 。然后只需使用 .text 或 get_attribute('textContent') 就可以了。

【讨论】:

  • 感谢您的回复。现在使用该代码我收到错误消息:selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@ngclick="getCandidateFolio(candidate.folio)"]"} 每张卡片都有 1 个我想要查找的作品集,但我想从页面上的所有卡片中查找作品集,find_element 是最好的方法吗?
  • 忘记了 - 并且 find_elements 将是那个。
  • 还是没有运气,我也有同样的信息。我可以改用什么查找方法?
  • 它在任何 iframe 中吗?
【解决方案2】:

我明白了!使用:

folios =  driver.find_elements_by_class_name('record_folio')

最重要的是必须添加一个 time.sleep(30) 因为页面显然没有完全加载。谢谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 2018-03-25
    • 2019-03-23
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多