Search code examples
pythonselenium-webdrivertooltip

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). 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. 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:

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:

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.

EDIT 2: Here is the relevant 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. It works with chromedriver, but only if I first hover over the tooltips, and only if I allow time.sleep(), as in

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)`

Solution

  • The reason it is returning the same element, because xpath is not changing for all the loop iterations.

    Two ways to deal:

    Use array notation for xpath as described below:

    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

    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.