0

Having trouble figuring out the next step, trying to download a pdf file from a website and getting stuck.

"https://www.southtechhosting.com/SanJoseCity/CampaignDocsWebRetrieval/Search/SearchByElection.aspx"

Page with Links to PDF Files

PDF file to download

I was able to click on the pdf link from the "Page with Links" using Selenium & ChromeDriver but then I get a popup form instead of a download.

I tried disabling the Chrome PDF Viewer ("plugins.plugins_list":[{"enabled":False,"name":"Chrome PDF Viewer"}]), but that doesn't work.

The popup form (viewed in "PDF file to download") has a hover link to download the pdf file. I've tried ActionChains(), but I get this exception after running this line:

from selenium.webdriver.common.action_chains import ActionChains

element_to_hover = driver.find_element_by_xpath("//paper-icon-button[@id='download']")
hover = ActionChains(driver).move_to_element(element_to_hover)
hover.perform()

enter image description here

Looking for the most efficient way to download pdf files in this type of situation. Thanks!

Glenn G.
  • 419
  • 3
  • 7
  • 18
  • Check [here](https://stackoverflow.com/questions/41877155/disabling-pdf-viewer-plugin-in-chromedriver) and disable the pdf viewer in chrome. Then when you click on the element which opens pop-up, will give you the link to the actual file. Open that link in new tab and it should download the file. – Kamal Dec 03 '18 at 05:36
  • Already tried this and it doesn't work. I get a blank form and no download – Glenn G. Dec 03 '18 at 05:53
  • What did not work for you, disabling the pdf viewer or the later part? When disabled pdf viewer, I can see such element `Click here` which when opened in new tab, downloads the file. – Kamal Dec 03 '18 at 06:03
  • I see what you mean, but I get the same issue when I try to access elements in the popup form. `driver.find_element_by_link_text("Click Here").click()` --> NoSuchElementException: unable to locate "Click Here"... – Glenn G. Dec 03 '18 at 06:24

1 Answers1

1

Please try this:

chromeOptions = webdriver.ChromeOptions()
prefs = {"plugins.always_open_pdf_externally": True}
chromeOptions.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chromeOptions)
driver.get('https://www.southtechhosting.com/SanJoseCity/CampaignDocsWebRetrieval/Search/SearchByElection.aspx')

#Code to open the pop-up
driver.find_element_by_xpath('//*[@id="ctl00_DefaultContent_ASPxRoundPanel1_btnFindFilers_CD"]').click()
driver.find_element_by_xpath('//*[@id="ctl00_GridContent_gridFilers_DXCBtn0"]').click()
driver.find_element_by_xpath('//*[@id="ctl00_DefaultContent_gridFilingForms_DXCBtn0"]').click()

driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
a = driver.find_element_by_link_text("Click here")
ActionChains(driver).key_down(Keys.CONTROL).click(a).key_up(Keys.CONTROL).perform()

UPDATE: To exit the popup, you can try this:

driver.switch_to.default_content()
driver.find_element_by_xpath('//*[@id="ctl00_GenericPopupSizeable_InnerPopupControl_HCB-1"]/img').click()
Kamal
  • 2,384
  • 1
  • 13
  • 25
  • Worked like a charm, need to get used to xpath. Thanks for the help! – Glenn G. Dec 03 '18 at 06:45
  • Quick question, after downloading the pdf file how do you exit the popup? I tried `ActionChains(driver).send_keys(Keys.ESCAPE).perform()` but I think the download did some shifting in frames. Is there a more elegant approach to closing the popup window after download? – Glenn G. Dec 03 '18 at 07:39