如何从JSF pag自动下载PDF文件

2024-04-26 06:43:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要使用python和selenium自动下载一些pdf文档。chrome和firefox的首选项已经实现了自动下载,但是我对这个特殊的网站有一个问题,它不会自动下载文档,它会打开一个新的选项卡

website: http://sistemas.sefaz.ma.gov.br/certidoes/jsp/emissaoCertidaoNegativa/emissaoCertidaoNegativa.jsf Select CPF/CNPJ and use this number to access the document 22977333000108.

填写完表单后,我在一个新的标签页中获得了与截图Documment Screenshot相同的url的文档(抱歉,必须隐藏文档摘要编号)。如果我手动下载它,并重命名为PDF文件,它工作得很好。在

如何将其下载为带有selenium的pdf文件?在

我的代码是:

 urlForm = 'https://sistemas.sefaz.ma.gov.br/certidoes/jsp/emissaoCertidaoNegativa/emissaoCertidaoNegativa.jsf'
 #loading firefox webdriver with preferences
 driver = loadDriver( urlForm ) 

 #form filled

 # Clicking submit button
 btnSubmit = driver.find_element_by_id('form1:j_id28')
 btnSubmit.click()
 time.sleep(3)     

 # switch to new tab
 driver.switch_to.window(driver.window_handles[-1]) 

然后我试着用glob下载文件,如下所示:

^{pr2}$

但它会打印出如下内容:

[emissaoCertidaoNegativa.jsf;jsessionid=44297D6C88452FC479FC0E94013D3C0A]

当我试图将它重命名为pdf时,我得到了一个空文件。。。有人能帮忙吗?在


Tags: 文件to文档brpdfdriverseleniumfirefox
1条回答
网友
1楼 · 发布于 2024-04-26 06:43:41

{根据^ a1:

Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification. pathname can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like ../../Tools//.gif), and can contain shell-style wildcards. Broken symlinks are included in the results (as in the shell).

所以必须指定文件的路径。在

示例:

假设我们在两个不同的目录中有两个output.txt文件:

  • C:\\Test\\output.txt
  • C:\\Users\\userName\\Desktop\\output.txt

如果我们尝试以下代码:

print(glob.glob('C:\\Test\\output.txt', recursive=True))
print(glob.glob('C:\\**\\output.txt', recursive=True))
print(glob.glob('C:\\**\\output.txt'))
print(glob.glob('output.txt'))

输出将是:

^{pr2}$

所以必须指定保存文件的目录的路径。您可以使用**,但如文档所述:

Note Using the “**” pattern in large directory trees may consume an inordinate amount of time.

相关问题 更多 >

    热门问题