Skip to content

Instantly share code, notes, and snippets.

@eliasalbuquerque
Last active January 15, 2024 12:02
Show Gist options
  • Save eliasalbuquerque/42bc918a83abbf75cecfd0813044d569 to your computer and use it in GitHub Desktop.
Save eliasalbuquerque/42bc918a83abbf75cecfd0813044d569 to your computer and use it in GitHub Desktop.
Curso Dev Aprender | Jhonatan de Souza

Desafio

  1. Navegue até o site https://cursoautomacao.netlify.app/
  2. Encontre e clique no campo "Digite seu nome" dentro de "exemplos Alertas" e digite seu nome
  3. Clique em alerta, para gerar a alerta
  4. Feche a alerta
  5. Suba a página totalmente para cima
  6. Desça apenas o suficiente para conseguir chegar até a secção que contém os arquivos para o quais irá fazer o download e click no botão de download para realizar o downlaod dos arquivos excel e pdf.
  7. Depois de ter feito isso, crie uma alerta que diz "VOCÊ TERMINOU"

Código

# 202401 - Python 3.12.0
# 4.14 - Automação de sites com PyAutoGUI


import pyautogui
import webbrowser
import time
import logging
import logging.config


# configurando logging:
logging.config.fileConfig(fname='config.ini', disable_existing_loggers=False)
logger = logging.getLogger(__name__)


def abrir_site(site, https):
    try:
        webbrowser.open(https)
        logging.info('site aberto')
    except Exception as e:
        logging.error(f'ERRO: ao acessar o site {site}\n- {type(e).__name__}: {e}')
        pyautogui.alert(f'ERRO: Ocorreu um erro acessar o site {site}')


def scroll_site(site):
    try:
        pyautogui.click(1436,129, duration=.5)
        logging.info('clicou no site para ativar a janela')
        
        time.sleep(1)
        pyautogui.scroll(-700)
        logging.info('desceu ate o campo "Exemplos Alertas"')

    except Exception as e:
        logging.error(f'ERRO: ao scrolar o site {site}\n- {type(e).__name__}: {e}')
        pyautogui.alert(f'ERRO: Ocorreu um erro scrolar o site {site}')


def digite_seu_nome(nome):
    try:
        time.sleep(1)
        captcha = pyautogui.locateCenterOnScreen('assets/digite-seu-nome.png')
        pyautogui.click(captcha, duration=.5)
        pyautogui.write(nome)
        logging.info('digitou o nome no campo "Exemplos Alertas"')

    except Exception as e:
        logging.error(f'ERRO: ao digitar o nome\n- {type(e).__name__}: {e}')
        pyautogui.alert(f'ERRO: Ocorreu um erro ao digitar o nome')


def abrir_fechar_alerta():
    try:
        captcha = pyautogui.locateCenterOnScreen('assets/clicar-alerta.png')
        pyautogui.click(captcha, duration=.5)
        logging.info('clicou no botao de alerta')
        
        time.sleep(1)
        pyautogui.hotkey('space')
        logging.info('fechou o alerta')

    except Exception as e:
        logging.error(f'ERRO: ao abrir e fechar alerta\n- {type(e).__name__}: {e}')
        pyautogui.alert(f'ERRO: Ocorreu um erro ao abrir e fechar Alerta')
    

def sobe_desce():
    try:
        time.sleep(1)
        pyautogui.scroll(700)
        logging.info('subiu ate o inicio da pagina')

        time.sleep(1)
        pyautogui.scroll(-2000)
        logging.info('desceu até downloads de arquivos')

    except Exception as e:
        logging.error(f'ERRO: ao subir e descer a janela\n- {type(e).__name__}: {e}')
        pyautogui.alert(f'ERRO: Ocorreu um erro ao subir e descer a janela')
  
    
def downlaod_arquivos():
    
    def download():
        try:
            captchas = ['assets/download-excel.png', 'assets/download-pdf.png']

            for captcha in captchas:
                time.sleep(1)
                posicao = pyautogui.locateCenterOnScreen(captcha)
                time.sleep(1)
                pyautogui.hotkey('esc')
                pyautogui.moveTo(posicao, duration=.5)
                x, y = posicao
                pyautogui.click(x, y + 42, duration=.5)
                time.sleep(1)
                pyautogui.hotkey('esc')
                
                # extraindo o tipo de arquivo a partir de string
                arquivo = captcha.split('/')[1].split('-')[1].split('.')[0]
                
                logging.info(f'download do arquivo {arquivo}')

        except Exception as e:
            logging.error(f'ERRO: ao fazer download dos arquivos\n- {type(e).__name__}: {e}')
            pyautogui.alert(f'ERRO: Ocorreu um erro ao fazer download dos arquivos')
        
    def mover_arquivos():
        try:
            time.sleep(1)
            pyautogui.hotkey('win', 'e')
            time.sleep(.5)
            pyautogui.hotkey('alt', 'd')
            time.sleep(.5)
            pyautogui.write('Downloads')
            time.sleep(.5)
            pyautogui.hotkey('enter')
            time.sleep(.5)
            pyautogui.hotkey('ctrl', 'a')
            time.sleep(.5)
            pyautogui.hotkey('ctrl', 'x')
            time.sleep(.5)
            pyautogui.hotkey('alt', 'd')
            time.sleep(.5)
            pyautogui.write('C:/Users/elias/Workspace/python-automation/assets')
            time.sleep(.5)
            pyautogui.hotkey('enter')
            time.sleep(.5)
            pyautogui.hotkey('ctrl', 'v')
            time.sleep(2)
            pyautogui.hotkey('enter')
            time.sleep(2)
            pyautogui.hotkey('ctrl', 'w')
            
            logging.info('arquivos movidos para a pasta assets')
        except Exception as e:
            logging.error(f'ERRO: ao mover arquivos\n- {type(e).__name__}: {e}')
            pyautogui.alert(f'ERRO: Ocorreu um erro ao mover arquivos')
        
    download()
    mover_arquivos()


def finalizando_script():
    try:
        time.sleep(.5)
        pyautogui.alert('VOCÊ TERMINOU APLICAÇÃO')
        logging.info('alerta de finalizacao executado')
    except Exception as e:
        logging.error(f'ERRO: ao emitir alerta de finalizacao\n- {type(e).__name__}: {e}')
        pyautogui.alert(f'ERRO: Ocorreu um erro ao emitir o alerta de finalizacao')
    
    

# APLICACAO:

site = 'cursoautomacao'
endereco = 'https://cursoautomacao.netlify.app/'
nome = 'Elias Albuquerque'

try:
    logging.info('START APPLICATION...')

    # args: nome do site, endereco do site
    if __name__ == '__main__':
        abrir_site(site, endereco)
        scroll_site(site)
        digite_seu_nome(nome)
        abrir_fechar_alerta()
        sobe_desce()
        downlaod_arquivos()
        finalizando_script()
    
    logging.info('END APPLICATION')

except Exception as e:
    logging.error(f'ERRO: ao rodar a aplicacao:\n- {type(e).__name__}: {e}')
    pyautogui.alert(f'ERRO: Ocorreu um erro ao digitar o nome')

Adicionado ao projeto:

  • uso de funções;
  • lista;
  • tratamento de string a partir de itens de uma lista;
  • tratamento de erros;
  • log da aplicação;

Resultado Final

automacao-completa

Log:

2024-01-03 16:01:42,598 - root - INFO - START APPLICATION...
2024-01-03 16:01:42,657 - root - INFO - site aberto
2024-01-03 16:01:43,330 - root - INFO - clicou no site para ativar a janela
2024-01-03 16:01:46,434 - root - INFO - desceu ate o campo "Exemplos Alertas"
2024-01-03 16:01:47,476 - PIL.PngImagePlugin - DEBUG - STREAM b'IHDR' 16 13
2024-01-03 16:01:47,476 - PIL.PngImagePlugin - DEBUG - STREAM b'sRGB' 41 1
2024-01-03 16:01:47,476 - PIL.PngImagePlugin - DEBUG - STREAM b'gAMA' 54 4
2024-01-03 16:01:47,476 - PIL.PngImagePlugin - DEBUG - STREAM b'pHYs' 70 9
2024-01-03 16:01:47,476 - PIL.PngImagePlugin - DEBUG - STREAM b'iTXt' 91 95
2024-01-03 16:01:47,477 - PIL.PngImagePlugin - DEBUG - STREAM b'IDAT' 198 1093
2024-01-03 16:01:48,398 - root - INFO - digitou o nome no campo "Exemplos Alertas"
2024-01-03 16:01:48,429 - PIL.PngImagePlugin - DEBUG - STREAM b'IHDR' 16 13
2024-01-03 16:01:48,429 - PIL.PngImagePlugin - DEBUG - STREAM b'sRGB' 41 1
2024-01-03 16:01:48,429 - PIL.PngImagePlugin - DEBUG - STREAM b'gAMA' 54 4
2024-01-03 16:01:48,429 - PIL.PngImagePlugin - DEBUG - STREAM b'pHYs' 70 9
2024-01-03 16:01:48,429 - PIL.PngImagePlugin - DEBUG - STREAM b'iTXt' 91 93
2024-01-03 16:01:48,429 - PIL.PngImagePlugin - DEBUG - STREAM b'IDAT' 196 604
2024-01-03 16:01:49,233 - root - INFO - clicou no botao de alerta
2024-01-03 16:01:50,335 - root - INFO - fechou o alerta
2024-01-03 16:01:51,437 - root - INFO - subiu ate o inicio da pagina
2024-01-03 16:01:52,538 - root - INFO - desceu até downlods de arquivos
2024-01-03 16:01:53,567 - PIL.PngImagePlugin - DEBUG - STREAM b'IHDR' 16 13
2024-01-03 16:01:53,568 - PIL.PngImagePlugin - DEBUG - STREAM b'sRGB' 41 1
2024-01-03 16:01:53,568 - PIL.PngImagePlugin - DEBUG - STREAM b'gAMA' 54 4
2024-01-03 16:01:53,568 - PIL.PngImagePlugin - DEBUG - STREAM b'pHYs' 70 9
2024-01-03 16:01:53,568 - PIL.PngImagePlugin - DEBUG - STREAM b'iTXt' 91 93
2024-01-03 16:01:53,568 - PIL.PngImagePlugin - DEBUG - STREAM b'IDAT' 196 1194
2024-01-03 16:01:57,222 - root - INFO - downloado do arquivo excel
2024-01-03 16:01:58,263 - PIL.PngImagePlugin - DEBUG - STREAM b'IHDR' 16 13
2024-01-03 16:01:58,263 - PIL.PngImagePlugin - DEBUG - STREAM b'sRGB' 41 1
2024-01-03 16:01:58,263 - PIL.PngImagePlugin - DEBUG - STREAM b'gAMA' 54 4
2024-01-03 16:01:58,263 - PIL.PngImagePlugin - DEBUG - STREAM b'pHYs' 70 9
2024-01-03 16:01:58,263 - PIL.PngImagePlugin - DEBUG - STREAM b'iTXt' 91 93
2024-01-03 16:01:58,263 - PIL.PngImagePlugin - DEBUG - STREAM b'IDAT' 196 787
2024-01-03 16:02:01,896 - root - INFO - downloado do arquivo pdf
2024-01-03 16:02:12,654 - root - INFO - arquivos movidos para a pasta assets
2024-01-03 16:02:16,163 - root - INFO - alerta de finalizacao executado
2024-01-03 16:02:16,163 - root - INFO - END APPLICATION

GitHub: eliasalbuquerque/python-automation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment