How to set a variable in the first call to a function, and keep this value in the second call on

Asked

Viewed 29 times

1

I have a function that arrow a variable after going through an extensive list, which takes a certain amount of time. The program calls this function other times because it is responsible for filling out a form. After this variable set the first time, it no longer needs to go through the list again, that is, the time spent the first time does not need to be lost again. This variable is equivalent to filling in a certain field of this form. How to make the program "remember" its value from the second time onwards?

def SetaConsulta(): #Essa consulta cria a lista dos processos com prazo em status "aguardando abertura".
    menu = browser.find_elements_by_class_name('ms-choice')
    menu[0].click()
    opcoes = browser.find_elements_by_tag_name('label')
    localidade = browser.find_element_by_id('selIdLocalidade')
    for x in range(len(opcoes)):
        if opcoes[x].text == "Curitiba":
            curitiba = x
        if opcoes[x].text == "Paranaguá":
            paranagua = x
    opcoes[curitiba].click() #Seleciona Curitiba na lista.
    opcoes[paranagua].click() #Seleciona Paranagua na lista.
    grau = Select(browser.find_element_by_id('selGrauJudicial'))
    grau.select_by_index(1)
    prazo = Select(browser.find_element_by_id('selPrazo'))
    prazo.select_by_index(1)
    dtInicio = browser.find_element_by_id('txtDataInicio')
    dtInicio.send_keys(dataInicio)
    dtFim = browser.find_element_by_id('txtDataTermino')
    dtFim.send_keys(dataFim) #dataFim
    inclusao = browser.find_element_by_id('selInclusaoProcurador')
    opcoesInclusao = inclusao.find_elements_by_tag_name('option')
    checkbox1 = browser.find_element_by_id('optChkPoloOposto')
    testeCheckbox1 = browser.find_element_by_id('optChkPoloOposto').is_selected()
    if testeCheckbox1 == False:
        checkbox1.click()
    checkbox2 = browser.find_element_by_id('optChkProcuradoresAssociados')
    testeCheckbox2 = browser.find_element_by_id('optChkProcuradoresAssociados').is_selected()
    if testeCheckbox2 == False:
        checkbox2.click()
    checkbox3 = browser.find_element_by_id('optChkExibirApensos')
    testeCheckbox3 = browser.find_element_by_id('optChkExibirApensos').is_selected()
    if testeCheckbox3 == False:
        checkbox3.click()
    checkbox4 = browser.find_element_by_id('optChkCpfCnpjSeparado')
    testeCheckbox4 = browser.find_element_by_id('optChkCpfCnpjSeparado').is_selected()
    if testeCheckbox4 == False:
        checkbox4.click()
    botao = browser.find_element_by_id('btnConsultar')
    botao.click()
    time.sleep(8)
    MontaDados()

I refer to the "Uritiba" and "Paranagua" variables of the above function, which are selected in a dropdown menu among several checkboxes.

1 answer

1


You can create global variables curitiba and paranagua, as follows:

curitiba = -1
paranagua = -1

def SetaConsulta(): #Essa consulta cria a lista dos processos com prazo e...
    ....

Later on, before:

        for x in range(len(opcoes)):
            if opcoes[x].text == "Curitiba":
                curitiba = x
            if opcoes[x].text == "Paranaguá":
                paranagua = x

place:

    if curitiba == -1 and paranagua == -1:
  • 1

    I had done it, but it hadn’t worked out... but I had set them with "None".

  • Maybe so, but then the test has to be if curitiba is None and paranagua is None:

Browser other questions tagged

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