Python: select checkbox in an orderly manner selenium web scraping

0

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 last one is the CURRENT person.

I also have another list with all POSSIBLE RESPONSIBLE.

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

I thought of doing the following:

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

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

3 - Click on the respective checkbox;

4 - Finish the distribution.

I have done the following function, but it does not 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 your help to come up with a better reasoning.

    
asked by anonymous 31.08.2017 / 21:18

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()
    
05.10.2017 / 23:09