Download pdf with python Selenium and Chrome

Asked

Viewed 456 times

0

I have a script that enters a site by Selenium, click on a button that generates the pdf on the site, through the code below I made this pdf be downloaded automatically, but stopped working a few weeks ago, which I need to change so I downloaded the pdf again, I did some tests but nothing worked.

Follow my script:

    **

chrome_options = Options()
        chrome_options.add_argument("--disable-notifications")
        #localização dos donwloads
        download_dir = "C:/downloads"

        chrome_options.add_experimental_option('prefs', {
               "plugins.plugins_list": [{"enabled":False,"name":"Chrome PDF Viewer"}],
               "download": {
               "prompt_for_download": False,
               "default_directory"  : download_dir
               }
        })


More of the script

**

driver = webdriver.Chrome('C:\scrapy\chromedriver', chrome_options=chrome_options)
driver.get('https://servicosonline.cpfl.com.br/agencia-webapp/#/login')
sleep(15)
print(codigo)

#Digitar email
username = driver.find_element_by_id('documentoEmail')
username.send_keys('email')
sleep(0.5)

#Digitar Senha
password = driver.find_element_by_id('password')
password.send_keys('senha')
sleep(2)

#Clicar botao para logar
sign_in_button = driver.find_element_by_xpath('//*[@valeutype="submit"]')
driver.execute_script("arguments[0].click();", sign_in_button)
sleep(5)
element = WebDriverWait(driver, 600).until(EC.presence_of_element_located((By.ID, 'selInstalacao')))

#Localizar botão Para procurar unidade
search_button = driver.find_element_by_xpath('//*[@type="search"]')
search_button.click()
sleep(4)

#Achar Unidades
original_dir = 'M:/Energia/Faturas Pendentes de Lançamento/Matão/{}'.format(codigos[c])
txt = 'M:/Energia/Faturas Pendentes de Lançamento/Matão/{}'.format(codigos[c])

actions = ActionChains(driver)
actions.send_keys('unidade')
sleep(1) # Pressionar o C
actions.perform()
sleep(2)

search_button = driver.find_element_by_xpath('//*[@type="search"]')
search_button.click()
sleep(4)

teste = driver.find_element_by_xpath('//*[@class="ui-select-choices-row ng-scope active"]')
driver.execute_script("arguments[0].click();", teste)


actions = ActionChains(driver)
actions.send_keys('{}'.format(codigos[c]))
sleep(1) # Pressionar o C
actions.perform()
sleep(2)


acessar = driver.find_element_by_xpath('//*[@class="btn btn-default btn-lg btn-block ng-scope"]')
acessar.click()
sleep(20)

#clicar logo cpfl
logocpfl = driver.find_element_by_xpath('//*[@class="navbar-logo-empresa"]')
logocpfl.click()
            sleep(10)

element = WebDriverWait(driver, 360).until(EC.element_to_be_clickable((By.ID, 'btnSegundaVia')))
segunda_via = driver.find_element_by_id('btnSegundaVia')
driver.execute_script("arguments[0].click();", segunda_via)


element = WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.ID, 'debitosPendentes')))
pdf = driver.find_elements_by_xpath('//*[@class="btn btn-default btn-segunda-via-aberta ng-scope"]')
driver.execute_script("arguments[0].click();", pdf[0])
sleep(5)

element = WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.ID, 'btnVerContaCompleta')))  
conta_completa = driver.find_element_by_id('btnVerContaCompleta')
conta_completa.click()
sleep(30)



driver.quit()
  • 1

    If it has stopped working from nothing, it may be that the site has changed some part of the HTML.. Submit more information about your code..

  • I edited and put more parts of the code, but the site does all the correct procedures, it just doesn’t download

  • It would be interesting to put the html code of this page in order to analyze it better.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.