循环多个工具提示
Looping over multiple tooltips
我正在尝试从 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 无法从所有工具提示中获取信息,而是返回:
author
#['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,三个元素不一样,所以我不明白为什么selenium不区分它们。
编辑 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)`
它返回相同元素的原因是 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')
。
我正在尝试从 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 无法从所有工具提示中获取信息,而是返回:
author
#['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,三个元素不一样,所以我不明白为什么selenium不区分它们。
编辑 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)`
它返回相同元素的原因是 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')
。