简体   繁体   中英

Selenium: a pop-up window not showing up in window handles

I am trying to get to a BibTeX entry of a research paper on Google Scholar using Selenium. For example, when one goes to this page, the top paper has the clickable quotes image below it. When one clicks the image, the citation window pops up. I would like to get the page source for that citation window using Selenium. However, I am not able to get to the pop-up window.

The relevant (at least that's how it seems) HTML element of the above page looks as follows:

<a href="javascript:void(0)" class="gs_or_cit gs_nph" title="Cite" role="button" aria-controls="gs_cit" aria-haspopup="true"><svg viewBox="0 0 17 16" class="gs_or_svg"><path d="M1.5 3.5v5h2v.375L1.75 12.5h3L6.5 8.875V3.5zM9.5 3.5v5h2v.375L9.75 12.5h3L14.5 8.875V3.5z"/></svg></a>

Here is what I am trying:

#!/usr/bin/python
from selenium import webdriver
import time
driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
link = u'https://scholar.google.co.il/scholar?hl=en&as_sdt=0%2C5&q=Enhanced+Partial+Expansion+%7BA%7D&btnG=&oq=enhanced+'
driver.set_window_size(1124, 850) # Avoid the error of the element not being displayed, see https://github.com/ariya/phantomjs/issues/11637
driver.get(link)
element = driver.find_element_by_class_name('gs_or_cit') # 'gs_or_cit gs_nph' contains space and is not accepted. However, 'gs_or_cit' seems to work.
element.click()
time.sleep(5) # Enough time for sure for the citation window to appear
print len(driver.window_handles)

This code prints out 1. That is, there is only one window handle and the citation window's handle is not available. Why would that be the case and how can I get to that window and its page source?

That's not a window, that's just a HTML dialog. You can treat it like any other HTML on the page and get its contents.

The parent element of that dialog is

<div id="gs_cit" class="gs_md_d gs_ttzi gs_vis" role="dialog" tabindex="-1" aria-labelledby="gs_cit-t" data-wfc="gs_cit-x" style="top: 253.5px;">

You can get it using the ID. You should wait for it to be visible and then get what contents you want from the dialog.

As to your code comment,

'gs_or_cit gs_nph' contains space and is not accepted. However, 'gs_or_cit' seems to work.

That is a compound class name ... meaning it contains more than one class. You can easily use a CSS selector, .gs_or_cit.gs_nph to find elements with those class names.

CSS Selector guide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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