jeudi 6 mai 2021

How to perform an action when an error occurs in the itertuple loop

I have the code below running fine. However, when an error occurs on the interaction page, the robot stops and takes no action.

I need that in case of any error, example: The url in question is down, I need the robot to stop and close the browser.

I tried to use

While True:
      try:
      except:

But when a page error occurred, the robot just stopped and did not close the browser

for row in df2.itertuples():
    r.frame('ifrm-pdr-portal')
    r.type('sc_clone_placa', row[1])
    r.type('id_sc_field_renavam', str(row[2]))
    r.click('sub_form_b')
    r.wait(10.0)
    p.scroll(-1400)
    r.wait(5.0)
    localPesq4 = p.locateOnScreen('GerarGuia.PNG', confidence=0.9)
    if localPesq4:
        r.frame('ifrm-pdr-portal')
        r.click('sc_pagar_bot')
        r.wait(5.0)
        r.frame('ifrm-pdr-portal')
        r.click('sc_download_bot')
        r.wait(5.0)
        janela = p.getActiveWindow()
        janela.maximize()
        p.sleep(2)
        p.click(x=391, y=36)
        p.hotkey('ctrl', 'c')
        url = pyperclip.paste()
        text = row[1] + str(row[2]) + '.pdf'
        r.download(url, text)
        upload_file = text
        gfile = drive.CreateFile({'parents': [{'id': '11SsSKYEATgn_VWzSb-8RjRL-VoIxvamC'}]})
        gfile.SetContentFile(upload_file)
        gfile.Upload()
        extrpdf = extract_text(text)
        new_csv = 'dados.csv'
        with open(new_csv, 'w', newline='', encoding='utf-8') as csvfile:
            file_writer = csv.writer(csvfile, delimiter=' ', quotechar=' ')
            file_writer.writerow(extrpdf)
        wb = Workbook()
        ws = wb.active
        with open('dados.csv', 'r') as f:
            for row2 in csv.reader(f, delimiter=';', quotechar=' '):
                try:
                    ws.append(row2)
                except IllegalCharacterError:
                    ws.append([str(i).encode('unicode_escape').decode('utf-8') for i in row2])
        wb.save('dados.xlsx')
        wb = openpyxl.load_workbook('dados.xlsx')
        sheet = wb.active
        venc = sheet['A25'].value
        valor = sheet['A24'].value
        p.sleep(1)
        worksheet.update('C' + str(lastRow + 1), [[venc, valor]])
        p.click(x=368, y=87)
        janela = p.getActiveWindow()
        janela.close()
        p.sleep(2)
        r.close()
        r.init()
        r.url('https://www.meudetran.ms.gov.br/veiculo.php#')
        p.sleep(2)
        janela = p.getActiveWindow()
        janela.maximize()
        p.sleep(2)
    else:
        r.close()
        r.init()
        r.url('https://www.meudetran.ms.gov.br/veiculo.php#')
        p.sleep(2)
        janela = p.getActiveWindow()
        janela.maximize()
        p.sleep(2)
p.sleep(5)
r.close()

Aucun commentaire:

Enregistrer un commentaire