Selenium script execution does not run when executed by another script

65 Views Asked by At

I have a process where I execute some routines in Python, for this I consult a table in BigQuery and then I bring the module and the function to be called, but specifically, the routines I am using in Selenium, it has the following problem

Exception in thread Thread-1 (run_script):
Traceback (most recent call last):
  File "C:\Users\901489108\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "C:\Users\901489108\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "P:\COMUM\FALCAO\30. EBB\00_project_Jasper\teste_orquestrador_v2.py", line 103, in run_script
    schedule_rotinas_pendentes()
  File "P:\COMUM\FALCAO\30. EBB\00_project_Jasper\teste_orquestrador_v2.py", line 95, in schedule_rotinas_pendentes
    executar_rotina(rotina,horario)
  File "P:\COMUM\FALCAO\30. EBB\00_project_Jasper\teste_orquestrador_v2.py", line 71, in executar_rotina
    getattr(m,funcao)()
  File "P:\COMUM\FALCAO\30. EBB\00_project_Jasper\services_v1_0_firefox.py", line 194, in main_script
    declarando_variaveis_globais()
  File "P:\COMUM\FALCAO\30. EBB\00_project_Jasper\services_v1_0_firefox.py", line 34, in declarando_variaveis_globais
    driver = webdriver.Firefox(options = options) # declara o driver do Firefox
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\901489108\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 67, in __init__
    super().__init__(command_executor=executor, options=options)
  File "C:\Users\901489108\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 205, in __init__
    self.start_session(capabilities)
  File "C:\Users\901489108\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 289, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\901489108\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 344, in execute
    self.error_handler.check_response(response)
  File "C:\Users\901489108\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response
    raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message: <!-- IE friendly error message walkround.        
     if error message from server is less than   
     512 bytes IE v5+ will use its own error     
     message instead of the one returned by      
     server.                                 --> 
                                                 
                                                 
                                                 
                                                 
                                                 
                                                 
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=8; IE=EDGE">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style type="text/css">
            body {
                height: 100%;
                font-family: Helvetica, Arial, sans-serif;
                color: #6a6a6a;
                margin: 0;
                display: flex;
                align-items: center;
                justify-content: center;
            }
            input[type=date], input[type=email], input[type=number], input[type=password], input[type=search], input[type=tel], input[type=text], input[type=time], input[type=url], select, textarea {
                color: #262626;
                vertical-align: baseline;
                margin: .2em;
                border-style: solid;
                border-width: 1px;
                border-color: #a9a9a9;
                background-color: #fff;
                box-sizing: border-box;
                padding: 2px .5em;
                appearance: none;
                border-radius: 0;
            }
            input:focus {
                border-color: #646464;
                box-shadow: 0 0 1px 0 #a2a2a2;
                outline: 0;
            }
            button {
                padding: .5em 1em;
                border: 1px solid;
                border-radius: 3px;
                min-width: 6em;
                font-weight: 400;
                font-size: .8em;
                cursor: pointer;
            }
            button.primary {
                color: #fff;
                background-color: rgb(47, 113, 178);
                border-color: rgb(34, 103, 173);
            }
            .message-container {
                height: 500px;
                width: 600px;
                padding: 0;
                margin: 10px;
            }
            .logo {
                background: url(https://globalurl.fortinet.net:8010/XX/YY/ZZ/CI/MGPGHGPGPFGHDDPFGGHGFHBGCHEGPFBGAHAH) no-repeat left center;
                height: 267px;
                object-fit: contain;
            }
            table {
                background-color: #fff;
                border-spacing: 0;
                margin: 1em;
            }
            table > tbody > tr > td:first-of-type:not([colspan]) {
                white-space: nowrap;
                color: rgba(0,0,0,.5);
            }
            table > tbody > tr > td:first-of-type {
                vertical-align: top;
            }
            table > tbody > tr > td {
                padding: .3em .3em;
            }
            .field {
                display: table-row;
            }
            .field > :first-child {
                display: table-cell;
                width: 20%;
            }
            .field.single > :first-child {
                display: inline;
            }
            .field > :not(:first-child) {
                width: auto;
                max-width: 100%;
                display: inline-flex;
                align-items: baseline;
                virtical-align: top;
                box-sizing: border-box;
                margin: .3em;
            }
            .field > :not(:first-child) > input {
                width: 230px;
            }
            .form-footer {
                display: inline-flex;
                justify-content: flex-start;
            }
            .form-footer > * {
                margin: 1em;
            }
            .text-scrollable {
                overflow: auto;
                height: 150px;
                border: 1px solid rgb(200, 200, 200);
                padding: 5px;
                font-size: 1em;
            }
            .text-centered {
                text-align: center;
            }
            .text-container {
                margin: 1em 1.5em;
            }
            .flex-container {
                display: flex;
            }
            .flex-container.column {
                flex-direction: column;
            }
        </style>
        <title>504 DNS look up failed</title>
    </head>
    <body><div class="message-container">
    <div class="logo"></div>
    <h1>504 DNS look up failed</h1>
    <p>The webserver reported that an error occurred while trying to access the website. Please return to the previous page.</p>
    <table><tbody>
        <tr>
            <td>URL</td>
            <td>http://localhost:52467/session</td>
        </tr>
    </tbody></table>
</div></body>
</html>

Running this script directly through it, it goes smoothly, however when I run it through another I get the problem above, follow the script that calls the error:

def executar_rotina(rotina,horario):
    # Lógica para executar a rotina
    rotina_nome = rotina['rotina']
    print(f"Executando rotina: {rotina_nome}")
    
    # Executar a função correspondente ao valor recebido do BigQuery
    try:
        #eval(rotina_nome)()
        # Quebra o campo em duas partes
        modulo, funcao = rotina_nome.split(' ')

        path = importlib.util.find_spec(modulo) 
        m = path.loader.load_module() 
        getattr(m,funcao)()
    except NameError:
        print(f"A função {rotina_nome} não existe.")

and the selenium script is as follows

# ------------------------------------------------- WEB SCRAPING - Services ------------------------------------------------- #
# Importando as bibliotecas
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import Select
from selenium import webdriver
from datetime import date, timedelta
from dateutil.relativedelta import relativedelta
from datetime import datetime
from datetime import timedelta
from pathlib import Path
from selenium import webdriver
import schedule as sc
import time
import mouse as mouse
import os
import os.path
import shutil
import pandas as pd
##import pyautogui as gui

# Não mexer - configuração dos proxies
os.environ['https_proxy'] = 'http://10.105.160.5:8080'

def declarando_variaveis_globais():
    global options, driver, action, delay, login, password, now_inicio_script, hora_atual_inicio_script, dia_anterior_hoje, dia_anterior_hoje_day, data_atual, ult_dia_mes_ant, ult_dia_mes_ant_day, ult_dia_mes_ant_month, ult_dia_mes_ant_year, dia_anterior_completo,hora_atual_proximo_script
    # Selenium Firefox
    options = webdriver.FirefoxOptions() # declara options do Firefox
    driver = webdriver.Firefox(options = options) # declara o driver do Firefox
    action = ActionChains(driver) # declara a função Action

    # Temporais
    # Lógicos
    delay = "30" # delay explícito (webDriverWait)
    periodo_extracao = 3 # periodo de extracao (em minutos)

    # Comparações temporais
    now_inicio_script = datetime.now() # data e hora atuais
    hora_atual_inicio_script = now_inicio_script.strftime('%d/%m/%Y %H:%M') # formato brasileiro da data e hora atuais
    hora_atual_proximo_script = now_inicio_script + relativedelta(minutes=3) # formato brasileiro da data e hora da próxima extração
    dia_anterior_hoje = date.today() - relativedelta(days=1) # Parâmetro D-1
    dia_anterior_hoje_day = dia_anterior_hoje.strftime('%d') # Parâmetro D-1 Day
    data_atual = date.today() # data atual
    ult_dia_mes_ant = (data_atual + relativedelta(months=0)).replace(day=1) # - timedelta(days=1) # soma 1 mês, seta o dia para 1 e subtrai 1 dia
    ult_dia_mes_ant_day = ult_dia_mes_ant.strftime('%d') # dia M-1
    ult_dia_mes_ant_month = ult_dia_mes_ant.strftime('%m') # mês M-1
    ult_dia_mes_ant_year = ult_dia_mes_ant.strftime('%Y') # ano M-1
    dia_anterior_completo = ult_dia_mes_ant_day+"/"+ ult_dia_mes_ant_month +"/"+ult_dia_mes_ant_year # data M-1

def abrir_link():
    global options, driver
    print('Horário da execução: ', hora_atual_inicio_script)

    while True:
        try:
            # abrindo a services
            driver.get('https://dashboards.gruposervices.com.br/carrefour/auth.html')
            WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'inputEmail3')))

            # inserindo credenciais
            print('Inserindo credenciais!')
            WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'inputEmail3'))).send_keys(login)
            WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'inputPassword3'))).send_keys(password)
            WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'btn_entrar'))).click()
            break
        except:
            print('Erro ao tentar abrir a Services! Reiniciando processo de abertura!')
            time.sleep(3)
            continue

def navegacao_pagina_chamadas():
    global action
    print('Iniciando extração de chamadas!')
    while True:
        try:
            WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH,"//*[text()='Dashboard Hora']")))
            print('Selecionando guia Dashboard Hora!')
            WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH,"//*[text()='BANCO - CARREFOUR']"))).click()
            time.sleep(2)
            for i in range(1, 6, 1):
                action.send_keys(Keys.ARROW_DOWN).perform()
            time.sleep(0.5)
            action.send_keys(Keys.ENTER).perform()
            time.sleep(0.5)
            WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'data_inicial_horasac'))).click()
            time.sleep(0.5)
            action.send_keys(Keys.ARROW_UP).perform()
            time.sleep(0.5)
            action.key_down(Keys.SHIFT).perform()
            time.sleep(0.5)
            action.send_keys(Keys.ARROW_RIGHT).perform()
            time.sleep(0.5)
            action.send_keys(Keys.ARROW_RIGHT).perform()
            time.sleep(0.5)
            action.key_up(Keys.SHIFT).perform()
            time.sleep(0.5)
            action.send_keys(Keys.BACK_SPACE).perform()
            time.sleep(0.5)
            action.send_keys(dia_anterior_hoje_day).perform()
            time.sleep(0.5)
            WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'data_final_horasac'))).click()
            time.sleep(0.5)
            action.send_keys(Keys.ARROW_UP).perform()
            time.sleep(0.5)
            action.key_down(Keys.SHIFT).perform()
            time.sleep(0.5)
            action.send_keys(Keys.ARROW_RIGHT).perform()
            time.sleep(0.5)
            action.send_keys(Keys.ARROW_RIGHT).perform()
            time.sleep(0.5)
            action.key_up(Keys.SHIFT).perform()
            time.sleep(0.5)
            action.send_keys(Keys.BACK_SPACE).perform()
            time.sleep(0.5)
            action.send_keys(dia_anterior_hoje_day).perform()
            time.sleep(0.5)
            WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH,"//*[contains(@onclick,'analitico_hora()')]"))).click()
            print("EXTRAÇÃO CHAMADAS FINALIZADA") 
            break
        except NoSuchElementException:
            print('Algum elemento não pôde ser encontrado! Reiniciando página...')
            driver.close()

def nav_pagina_transferencias():
    global actions
    print('Iniciando extração de transferências!')
    while True:
        try:
            time.sleep(0.5)
            mouse.move(19, 415)
            mouse.click()
            mouse.wheel(-20)
            print("Selecionando o menu 'Extrações'!")
            WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH,"//*[text()='Extrações']"))).click()
            time.sleep(5)
            WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH,"//*[text()='BANCO - CARREFOUR']"))).click()
            time.sleep(2)
            for i in range(1, 6, 1):
                action.send_keys(Keys.ARROW_DOWN).perform()
            time.sleep(0.5)
            action.send_keys(Keys.ENTER).perform()
            time.sleep(0.5)
            WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'data_inicial_rel'))).click()
            time.sleep(2)
            action.send_keys(Keys.ARROW_UP).perform()
            time.sleep(0.5)
            action.key_down(Keys.SHIFT).perform()
            time.sleep(0.5)
            action.send_keys(Keys.ARROW_RIGHT).perform()
            time.sleep(0.5)
            action.send_keys(Keys.ARROW_RIGHT).perform()
            time.sleep(0.5)
            action.key_up(Keys.SHIFT).perform()
            time.sleep(0.5)
            action.send_keys(Keys.BACK_SPACE).perform()
            time.sleep(0.5)
            action.send_keys(dia_anterior_hoje_day).perform()
            time.sleep(2)
            WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'data_final_rel'))).click()
            time.sleep(2)
            action.send_keys(Keys.ARROW_UP).perform()
            time.sleep(0.5)
            action.key_down(Keys.SHIFT).perform()
            time.sleep(0.5)
            action.send_keys(Keys.ARROW_RIGHT).perform()
            time.sleep(0.5)
            action.send_keys(Keys.ARROW_RIGHT).perform()
            time.sleep(0.5)
            action.key_up(Keys.SHIFT).perform()
            time.sleep(0.5)
            action.send_keys(Keys.BACK_SPACE).perform()
            time.sleep(0.5)
            action.send_keys(dia_anterior_hoje_day).perform()
            time.sleep(5)
            WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'load_transferencia'))).click()
            time.sleep(20)
            print("extração finalizada")
           ## print(f'Próxima extração prevista para às: {hora_atual_proximo_script}')
            break
        except NoSuchElementException:
            print('Algum elemento não pôde ser encontrado! Reiniciando página...')
            driver.close()
    
def main_script():
    declarando_variaveis_globais()
    abrir_link()
    navegacao_pagina_chamadas()
    nav_pagina_transferencias()
    time.sleep(300)
    #gui.press('volumedown')
    time.sleep(300)
    driver.close()
    

I don't know what else to do, I tried everything to do it.

0

There are 0 best solutions below