返回

python-无头selenium下载不运行/保存

发布时间:2022-05-07 10:57:38 223
# node.js

我正在使用脚本将一些 PDF 文件下载到我计算机上的文件夹中。该脚本完美运行,但是,当使用 chrome active 的参数(“--headless”)运行相同的脚本时,文件不再像往常一样保存。

我不明白这个“无头”属性正在做什么来防止文件被保存。

下面是正在使用的代码:(运行脚本时不显示错误。)

######### looping if ctrl+p
print("Setando configurações...")
settings = {
    "recentDestinations": [{
            "id": "Save as PDF",
            "origin": "local",
            "account": "",
        }],
        "isLandscapeEnabled": True,
        "selectedDestinationId": "Save as PDF",
        "version": 2,
    }
prefs = {
    "printing.print_preview_sticky_settings.appState": json.dumps(settings),
    "profile.default_content_settings.popups" : 0,
    "savefile.default_directory": r'/home/file_name/directory_downloads/pdfs',# Para salvar com o 'headless' inativo
    "download.default_directory": r'/home/file_name/directory_downloads/pdfs', # Para salvar com o 'headless' ativo
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "safebrowsing.enabled": True
    }

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')
chrome_options.add_argument('--headless') # Se ativo, salva os arquivos em um diretório desconhecido ou sobrepões arquivos salvos

print('Abrindo navegador...')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get(dashObject.url)
driver.maximize_window()

print('Pausa para carregar o conteúdo da página')
sleep(7)
texto_xpath2 = driver.find_element_by_xpath('//*[@id="embedWrapperID"]/div[2]/logo-bar/div/div/div/logo-bar-navigation/span/a/span/span[3]').text
int_num_page2 = int(texto_navegacao_xpath2)

print('Preparando os arquivos PDFs...')
int_num_page = 1
while int_num_page <= int_num_page2:
    numero_pagina1 = str(int_num_page)

    driver.execute_script('window.print();')
    print(f'PDF da pagina {int_num_page} de {int_num_page2} OK')
    
    print('Proxima pagina...')
    xpath_next = '//*[@id="embedWrapperID"]/div[2]/logo-bar/div/div/div/logo-bar-navigation/span/button[2]'
    driver.find_element_by_xpath(xpath_next).click()
    sleep(3)

    print("Mudando nome do arquivo...")
    pasta_download = pasta_pdfs + '/'
    arquivo_pdf = max([pasta_download + f for f in os.listdir(pasta_download)],key=os.path.getctime)
    shutil.move(arquivo_pdf,os.path.join(pasta_download,r"file_name"+ numero_pagina1 + dashUUID + ".pdf"))
    
    int_num_page += 1

print('Fechando navegador...')
driver.quit()
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像