【问题标题】:Selenium download a pdf to a specified pathSelenium 将pdf下载到指定路径
【发布时间】:2021-12-30 10:50:51
【问题描述】:

我创建了一个程序,该程序将转到某个网站,输入一个作品集编号,然后进入页面并单击以生成 PDF。我现在需要将该 pdf 下载到指定路径。

我已经能够生成 PDF。但是我正在发送密钥,但它不起作用。

PDF文件地址:https://gisweb.miamidade.gov/SPTXLienLetters/ReportPage.aspx?folio=0131230371470&pSecurityGuardDistrict=&pStreetLightDistrict=&pMultiPurposeDistrict=&pMunicipalityForDistrict=MIAMI&pAddressForDistrict=1400%20NW%2044%20ST

用于转到页面并生成 pdf 的代码:

options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver.implicitly_wait(10)

#set chromedriver.exe path 
driver = webdriver.Chrome('driver/chromedriver.exe')
driver.maximize_window()
#driver.execute_script("document.body.style.zoom='75%'")

#Launch Url
driver.get('https://gisweb.miamidade.gov/SPTXLienLetters/')

driver.find_element_by_xpath('//*[@id="divSplashScreenContent"]/table/tbody/tr[2]/td/div/div').click()

#Take data from config file
file = open('configsa.txt')
lines = file.readlines()
folio_number = lines[0]

driver.implicitly_wait(30)

#Find elements and take snapshots
elementID = driver.find_element_by_xpath('//*[@id="txtAddress"]')
elementID.send_keys(folio_number)

elementID = driver.find_element_by_xpath('//*[@id="tdDivAddress"]/table/tbody/tr/td[2]').click()

elementID = driver.find_element_by_xpath('//*[@id="trOtherAppLinks"]/td/div/span[1]/a').click()

我尝试使用以下代码进行保存,但无济于事:

action_chains = ActionChains(driver)
action_chains.send_keys(Keys.CONTROL).send_keys('s').perform()

saveas = ActionChains(driver).key_down(Keys.CONTROL).send_keys('S').key_up(Keys.CONTROL).send_keys('MyDocumentName').key_down(Keys.ALT).send_keys('S').key_up(Keys.ALT)

以上代码均无效。有人可以帮忙吗?

【问题讨论】:

  • configsa.txt 文件中的数据是什么?我们需要一些 folio_number 数据以用于调试

标签: python selenium pdf selenium-webdriver


【解决方案1】:

单击“生成并打印财产留置权信”标签后,它会打开一个新选项卡,其中会显示生成的文档。
您必须切换到新选项卡,然后在此处执行 CONTROL + S 以保存文档,关闭新选项卡并切换回原始窗口:

window1 = driver.window_handles[0]
elementID = driver.find_element_by_xpath('//*[@id="txtAddress"]')
elementID.send_keys(folio_number)

elementID = driver.find_element_by_xpath('//*[@id="tdDivAddress"]/table/tbody/tr/td[2]').click()

elementID = driver.find_element_by_xpath('//*[@id="trOtherAppLinks"]/td/div/span[1]/a').click()
window2 = driver.window_handles[1]
driver.switch_to_window(window2)
action_chains = ActionChains(driver)
action_chains.send_keys(Keys.CONTROL).send_keys('s').perform()
driver.close()
driver.switch_to_window(window1)

【讨论】:

    【解决方案2】:

    当您单击Generate and Print Property Lien Letter 时,会启动一个新选项卡。 Selenium 对此一无所知,因此它希望您让 Selenium 知道您要切换到新标签:

    current_windows = driver.current_window_handle
    elementID = driver.find_element_by_xpath('//*[@id="trOtherAppLinks"]/td/div/span[1]/a').click()
    all_handles = driver.window_handles
    driver.switch_to.window(all_handles[1])
    print('Switch to new tab has been successful.')
    

    另外,CTRL + s 会提示一个下载框,您也必须编写非 selenium 代码来解决这个问题。

    您不应在代码中多次使用driver.implicitly_wait(30)。因为这是一个隐式等待,应该为整个 Web 驱动程序生命周期设置。

    【讨论】:

      【解决方案3】:

      您无法下载 pdf 的原因是当您打开 PDF chrome 或您的浏览器在 pdf 查看器中打开它时。此选项卡不再属于 selenium web 驱动程序的范围。您知道这一点是因为您无法在浏览器上进行检查并选择输入框来指定路径。 要下载文件,您可以使用更简单的方法。获得 PDF 网址后,您可以使用 urllib 库,如下所示

      import urllib.request
      pdf_url = "https://gisweb.miamidade.gov/SPTXLienLetters/ReportPage.aspx?folio=0131230371470&pSecurityGuardDistrict=&pStreetLightDistrict=&pMultiPurposeDistrict=&pMunicipalityForDistrict=MIAMI&pAddressForDistrict=1400%20NW%2044%20ST"
      download_path = "local_filepath/filename.pdf"
      urllib.request.urlretrieve(pdf_url, download_path)
      

      【讨论】:

      • 在与新标签交互​​之前,他永远不会有 pdf url。
      猜你喜欢
      • 1970-01-01
      • 2020-12-29
      • 2015-08-07
      • 2015-08-30
      • 1970-01-01
      • 2016-01-19
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多