1
**from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions



chrome_path = r"C:/chromedriver.exe"
chrome_profile = webdriver.ChromeOptions()
profile = {"download.default_directory": "C:\\Users\\adarshb061\\Desktop",
           "download.prompt_for_download": False,
           "download.directory_upgrade": True,
           "plugins.plugins_list": [{"enabled":False,"name":"Chrome PDF Viewer"}]}
chrome_profile.add_experimental_option("prefs", profile)
chrome_profile.add_argument("--disable-extensions")

browser = webdriver.Chrome(chrome_options=chrome_profile, executable_path=chrome_path)
browser.get("https://rbi.org.in/")
browser.maximize_window()
browser.find_element_by_xpath("""//*[@id="FEMA"]/a""").click()
browser.find_element_by_xpath("""//*[@id="FEMANotifications"]""").click()

def download_lnk(url):
    browser.get(url)
    browser.execute_script("window.history.go(-1)")

rows = browser.find_elements_by_xpath("//table[@class='tablebg']//tr//td/a[contains(@href,'http://')]")
for row in rows:
    pdf_link = row.get_attribute('href')
    print(pdf_link)
    download_lnk(pdf_link)**

It is downloading only one PDF file from the web table. After that is giving me below exception: selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

Please suggest? Thanks in advance!

adarsh2109
  • 109
  • 1
  • 11
  • For `StaleElementReferenceException` see this [QA/Discussion](https://stackoverflow.com/questions/44838538/staleelementreference-exception-in-pagefactory/44844879#44844879) – undetected Selenium Nov 21 '17 at 07:04
  • https://stackoverflow.com/questions/45097302/download-and-save-multiple-csv-files-using-selenium-and-python-from-popup this link is for download the multiple csv files simultaneously however I need to download the PDF row by row. – adarsh2109 Nov 21 '17 at 08:40
  • Doesn't the logic remains the same whether it's `csv` or `PDF` files? – undetected Selenium Nov 21 '17 at 08:42
  • I have to iterate row by row to get the PDF and get back to previous page. Link provided by you is to get the multiple csv file in one click only. – adarsh2109 Nov 21 '17 at 10:15

0 Answers0