循环多个工具提示

时间:2016-01-06 17:13:41

标签: python selenium-webdriver tooltip

我试图从this page的一系列文章中获取作者的姓名和隶属关系(您需要访问Proquest才能将其可视化)。我想要做的是打开页面顶部的所有工具提示,并从中提取一些HTML文本。这是我的代码:

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)

虽然n_authors有三个显然彼此不同的条目,但selenium无法从所有工具提示中获取信息,而是返回此信息:

  

作者

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

该机构也是如此。我错了什么?非常感谢

编辑:

包含工具提示的xpath的数组:

  

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}")>]

长度为3,三个元素不同,这就是为什么我不明白为什么硒不能区分它们。

编辑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>.

更新: @ parishodak的回答,由于某种原因无法使用Firefox,除非我首先手动将鼠标悬停在工具提示上。它适用于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)`

1 个答案:

答案 0 :(得分:2)

它返回相同元素的原因,因为xpath不会因所有循环迭代而改变。

两种处理方式:

对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')

而不是find_element_by_xpath使用find_elements_by_xpath

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

循环遍历元素并在循环迭代中对每个元素使用get_attribute('innerHTML')