简体   繁体   English

遍历多个工具提示

[英]Looping over multiple tooltips

I am trying to get names and affiliations of authors from a series of articles from this page (you'll need to have access to Proquest to visualise it). 我正在尝试从此页面上的一系列文章中获得作者的姓名和隶属关系(您需要访问Proquest才能对其进行可视化)。 What I want to do is to open all the tooltips present at the top of the page, and extract some HTML text from them. 我要做的是打开页面顶部显示的所有工具提示,并从中提取一些HTML文本。 This is my code: 这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

browser = webdriver.Firefox()

url = 'http://search.proquest.com/econlit/docview/56607849/citation/2876523144F544E0PQ/3?accountid=13042'
browser.get(url)

#insert your username and password here

n_authors = browser.find_elements_by_class_name('zoom') #zoom is the class name of the three tooltips that I want to open in my loop

author = []
institution = []    

for a in n_authors:
    print(a)
    ActionChains(browser).move_to_element(a).click().perform()
    html_author = browser.find_element_by_xpath('//*[@id="authorResolveLinks"]/li/div/a').get_attribute('innerHTML')
    html_institution = browser.find_element_by_xpath('//*[@id="authorResolveLinks"]/li/div/p').get_attribute('innerHTML')
    author.append(html_author)
    institution.append(html_institution)

Although n_authors has three entries that are apparently different from one another, selenium fails to get the info from all tooltips, instead returning this: 尽管n_authors具有三个显然彼此不同的条目,但是selenium无法从所有工具提示中获取信息,而是返回以下内容:

author 作者

#['Nuttall, William J.',
#'Nuttall, William J.',
#'Nuttall, William J.']

And the same happens for the institution. 对于机构来说也是如此。 What am I getting wrong? 我怎么了? Thanks a lot 非常感谢

EDIT: 编辑:

The array containing the xpaths of the tooltips: 包含工具提示的xpath的数组:

n_authors n_authors

#[<selenium.webdriver.remote.webelement.WebElement (session="277c8abc-3883-
#43a8-9e93-235a8ded80ff", element="{008a2ade-fc82-4114-b1bf-cc014d41c40f}")>,
#<selenium.webdriver.remote.webelement.WebElement (session="277c8abc-3883-      
#43a8-9e93-235a8ded80ff", element="{c4c2d89f-3b8a-42cc-8570-735a4bd56c07}")>,
#<selenium.webdriver.remote.webelement.WebElement (session="277c8abc-3883-  
#43a8-9e93-235a8ded80ff", element="{9d06cb60-df58-4f90-ad6a-43afeed49a87}")>]

Which has length 3, and the three elements are different, which is why I don't understand why selenium won't distinguish them. 它的长度为3,并且这三个元素不同,这就是为什么我不理解为什么硒无法区分它们的原因。

EDIT 2: Here is the relevant HTML 编辑2:这是相关的HTML

<span class="titleAuthorETC small">
  <span style="display:none" class="title">false</span>
  Jamasb, Tooraj
  <a class="zoom" onclick="return false;" href="#">
    <img style="margin-left:4px; border:none" alt="Visualizza profilo" id="resolverCitation_previewTrigger_0" title="Visualizza profilo" src="/assets/r20161.1.0-4/ctx/images/scholarUniverse/ar_button.gif">
  </a><script type="text/javascript">Tips.images = '/assets/r20161.1.0-4/pqc/javascript/prototip/images/prototip/';</script>; Nuttall, William J
  <a class="zoom" onclick="return false;" href="#">
    <img style="margin-left:4px; border:none" alt="Visualizza profilo" id="resolverCitation_previewTrigger_1" title="Visualizza profilo" src="/assets/r20161.1.0-4/ctx/images/scholarUniverse/ar_button.gif">
  </a>; Pollitt, Michael G
  <a class="zoom" onclick="return false;" href="#">
    <img style="margin-left:4px; border:none" alt="Visualizza profilo" id="resolverCitation_previewTrigger_2" title="Visualizza profilo" src="/assets/r20161.1.0-4/ctx/images/scholarUniverse/ar_button.gif">
  </a>.

UPDATE : @parishodak's answer, for some reason does not work using Firefox, unless I manually hover over the tooltips first. 更新 :@parishodak的答案,由于某些原因,在使用Firefox时不起作用,除非我先手动将鼠标悬停在工具提示上。 It works with chromedriver, but only if I first hover over the tooltips, and only if I allow time.sleep(), as in 它适用于chromedriver,但前提是我首先将鼠标悬停在工具提示上,并且仅当我允许time.sleep()时,如

for i in itertools.count():
    try:
        tooltip = browser.find_element_by_xpath('//*[@id="resolverCitation_previewTrigger_' + str(i) + '"]')
        print(tooltip)
        ActionChains(browser).move_to_element(tooltip).perform()    #
    except NoSuchElementException:
        break

time.sleep(2)

elements = browser.find_elements_by_xpath('//*[@id="authorResolveLinks"]/li/div/a')
author = []    

for e in elements:
    print(e)
    attribute = e.get_attribute('innerHTML')
    author.append(attribute)`

The reason it is returning the same element, because xpath is not changing for all the loop iterations. 之所以返回相同的元素,是因为在所有循环迭代中xpath都没有改变。

Two ways to deal: 两种处理方式:

Use array notation for xpath as described below: 如下所述,对xpath使用数组符号:

browser.find_elements_by_xpath('//*[@id="authorResolveLinks"]/li/div/a[1]').get_attribute('innerHTML')
browser.find_elements_by_xpath('//*[@id="authorResolveLinks"]/li/div/a[2]').get_attribute('innerHTML')
browser.find_elements_by_xpath('//*[@id="authorResolveLinks"]/li/div/a[3]').get_attribute('innerHTML')

Or 要么

Instead of find_element_by_xpath use find_elements_by_xpath 代替find_element_by_xpath使用find_elements_by_xpath

elements = browser.find_elements_by_xpath('//*[@id="authorResolveLinks"]/li/div/a')

loop over elements and use get_attribute('innerHTML') on each element in loop iteration. 循环遍历元素,并在循环迭代中对每个元素使用get_attribute('innerHTML')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM