4

I am working on an automation project and I am trying to download a pdf from a website. The website only contains the pdf but the file type of the webpage is HTML. The pdf is displayed using PDF.js and the PDF.js viewer is also in an iframe.

When I tried to click the element using browser javascript, i was returned with a security error relating to cross site scripting.

SecurityError: Permission denied to access property "document" on cross-origin object

I would like to download the pdf from my script, written in python, using selenium. When I try this:

driver.find_element_by_id('download').click()

No results are produced, the download button doesn't get clicked even though I have switched focus to the iframe in selenium.

Does anybody know a solution how to download the pdf?

koder613
  • 1,486
  • 5
  • 21

1 Answers1

1

To click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using ID:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "download"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#download"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='download']"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Reference

You can find a detailed discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I used your code: WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "download"))).click() However I now receive this error: 'Exception has occurred: ElementClickInterceptedException' How do I get around this? – koder613 Jun 30 '20 at 13:55
  • @EC Can you update the question with the complete error trace log please? – undetected Selenium Jun 30 '20 at 13:59
  • @EC `someId`, `someClass`, `someJS`, etc won't help me to improve the answer. The relevant html and the exact error trace log holds the key to a solution. – undetected Selenium Jun 30 '20 at 19:35
  • Modified question. Do you know how to avoid this error? – koder613 Jul 01 '20 at 11:21
  • @EC Locator in the question is `find_element_by_id('download')` where as the locator in the error is `(By.ID, "ctl00_PH_AvailableReport1_UCAvailRpt_dgAvailableReport_ctl00_ctl06_lnkRptOpt")`. Do you realize now how essential it is to publish the relevant text based HTML? – undetected Selenium Jul 01 '20 at 13:25