Python: select checkbox in an orderly way

Asked

Viewed 382 times

1

I have a list containing hundreds of data in the format

[
    '5008489', 
    'Órgão: MPF', 
    'PROCEDIMENTO DO JUIZADO ESPECIAL', 
    'CPF', 
    <selenium.webdriver.remote.webelement.WebElement (session="8834847081a4be257906cce85807f88a", element="0.34327825856075167-261")>, 
    'SERGIO AUGUSTO NOME FICTICIO'
]

Since item 0 is the process number, the penultimate item is the corresponding checkbox and the latter is the CURRENT responsible.

I also have another list with all the possible responsible.

Finally I have a list with several processes, which I need to REDISTRIBUTE TO NEW RESPONSIBLE.

I thought I’d do it this way:

1 - Identify, in the list of processes to be redistributed, who will receive new processes;

2 - From the first name, select all the processes that will go to it;

3 - Click on the respective checkbox;

4 - Finalising the distribution.

I did the following job, but it doesn’t seem satisfactory:

def DistribuiProcesso():
distribuir = Select(browser.find_element_by_id('listaResponsaveis'))
responsaveis = distribuir.options
for x in range(len(responsaveis)):
    for y in range(len(processosAlvo)): #Iterando a lista dos processos que serão redistribuidos...
        for z in range(len(processosAlvo[y])): #Iterando os itens de cada processo...
            if z == len(processosAlvo[y]) - 1: #Localizando o item que contém o nome do responsável...
                responsavelAlvo = processosAlvo[y][z]
                if responsavelAlvo == responsaveis[x].text:
                    if z == len(processosAlvo[y]) - 2: #Localizando o item que contém o checkbox... 
                        processosAlvo[y][z].click()

I ask for your help to devise a better reasoning.

1 answer

0

Solution found:

def DistribuiProcesso():
    try:
        log = open('c:\\E-Proc\\Distribuicao ' + str(hoje) + '.txt', 'w+') #Cria log de controle.
    except FileNotFoundError:
        log = open('c:\\E-Proc\\Distribuicao ' + str(hoje) + '.txt', 'w+')
    distribuir = Select(browser.find_element_by_id('selProNov771230778800100040000000000014')) #Abre o seletor dos Procuradores
    gerenciar = Select(browser.find_element_by_id('selTipo771230778800100040000000000014'))
    procuradores = distribuir.options
    for x in range(len(procuradores)):
        distribuir = Select(browser.find_element_by_id('selProNov771230778800100040000000000014')) #Abre o seletor dos Procuradores
        gerenciar = Select(browser.find_element_by_id('selTipo771230778800100040000000000014'))
        procuradores = distribuir.options
        nome = str(procuradores[x].text)
        if x != 0 and x != 1 and x != 26 and x != 63 and x != 94: #Regra interna de exceção.
            controle = 0
            listaProcessos = []
            for y in range(len(processosAlvo)): #Encontrando na lista os processos que correspondem ao procurador do laço.

                if processosAlvo[y][2] in nome: 
                    controle = controle + 1
                    processosAlvo[y][1].send_keys('\ue007')
                    processosAlvo[y][1].click()
                    listaProcessos.append(processosAlvo[y])

            if controle > 0:
                gerenciar.select_by_index(1)
                distribuir.select_by_index(x)
                for x in range(1):
                    try:
                        executar = browser.find_element_by_id('btnExecutar')
                        executar.send_keys('\ue007')
                        executar.click()
                        time.sleep(10)
                    except:
                        break
                log.writelines("Para o(a) procurador(a): " + nome)
                log.writelines('\n')
                for x in range(len(listaProcessos)):
                    log.writelines(str(listaProcessos[x][0]))
                    log.writelines('\n')
                voltar = browser.find_element_by_id('btnVoltar')
                voltar.click()
                time.sleep(4)                   
                LimpaVariaveis() #Limpa a base da informação.
                SetaConsulta() #Refaz as consultas e realimenta a base.
                log.writelines('\n')
    log.close()

Browser other questions tagged

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