Problem with Python Selenium

Asked

Viewed 458 times

1

I made a program to download 64 invoices, it goes well up to the ninth, but when Lenium arrives in it stops clicking without error

all Elif take a different id and perform the same action with it, but when it reaches the ninth Elif it hangs, but if I start straight from the 9 it works and hangs on the 17, I would like to know pq hangs from 9 in 9

He stops at this part of the code:

segunda_via = driver.find_element_by_xpath('//*[@class="card-vertical-align ng-scope"]')
    segunda_via.click()
    #sleep(10)

and that’s the rest of the code, having 63 Elif between the first if and Else, locking only on the ninth:

while a < 64: #Change Chrome driver options chrome_options = Options() #location of the Donwloads download_dir = "C: scrapy"

    #Novas configurações
    chrome_options.add_experimental_option('prefs', {
        "plugins.plugins_list": [{"enabled":False,"name":"Chrome PDF Viewer"}],
        "download": {
        "prompt_for_download": False,
        "default_directory"  : download_dir
        }
    })
    #Localizar webdriver no pc e entrar no site
    driver = webdriver.Chrome('C:\scrapy\chromedriver', chrome_options=chrome_options)
    driver.get('https://servicosonline.cpfl.com.br/agencia-webapp/#/login')
    sleep(15)
    element = WebDriverWait(driver, 600).until(EC.presence_of_element_located((By.ID, 'encontrarInstalacao')))
    #Clicar no checkbox do email
    email = driver.find_element_by_xpath('//*[@translate="@APP-COMMON-EMAIL"]')
    email.click()

    #Digitar email
    username = driver.find_element_by_id('documentoEmail')
    username.send_keys('email')
    sleep(0.5)

    #Digitar Senha
    password = driver.find_element_by_id('senha')
    password.send_keys('password')
    sleep(0.5)

    #Clicar botao para logar
    sign_in_button = driver.find_element_by_xpath('//*[@valeutype="submit"]')
    sign_in_button.click()
    sleep(2)
    element = WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.ID, 'selInstalacao')))

    #Localizar botão Para procurar unidade
    search_button = driver.find_element_by_xpath('//*[@type="search"]')
    search_button.click()
    sleep(0.5)

    #Achar Unidades
    if a == 1:

        actions = ActionChains(driver)
        actions.send_keys('25805681') # Pressionar o C
        actions.perform()

        teste = driver.find_element_by_xpath('//*[@class="ui-select-choices-row ng-scope active"]')
        teste.click()
        sleep(1)
     elif a == 9:      

    #trocar nome e pasta do arquivo
    os.rename('C:/scrapy/' + 'conta-completa.pdf','C:/scrapy/teste/' + '38085143.pdf')
    #trocar nome de arquivos da pasta
    original_dir = 'C:/scrapy/teste'
    for n, file_name in enumerate(os.listdir(original_dir), 1):
        full_path = os.path.join(original_dir, file_name)
        if (os.path.isfile(full_path) and '{}_'.format(n) not in file_name):
            os.rename(full_path, '{}/{}_{}'.format(original_dir, n, file_name))

    actions = ActionChains(driver)
    actions.send_keys('39615529')
    actions.perform()

    teste = driver.find_element_by_xpath('//*[@class="ui-select-choices-row ng-scope active"]')
    teste.click()
    sleep(1)

... goes to elif 64
else:      

        #trocar nome e pasta do arquivo
        os.rename('C:/scrapy/' + 'conta-completa.pdf','C:/scrapy/teste/' + '4001760792.pdf')
        #trocar nome de arquivos da pasta
        original_dir = 'C:/scrapy/teste'
        for n, file_name in enumerate(os.listdir(original_dir), 1):
            full_path = os.path.join(original_dir, file_name)
            if (os.path.isfile(full_path) and '{}_'.format(n) not in file_name):
                os.rename(full_path, '{}/{}_{}'.format(original_dir, n, file_name))
        sleep(2)


    #Clicar no acessar
    acessar = driver.find_element_by_xpath('//*[@class="btn btn-default btn-lg btn-block ng-scope"]')
    acessar.click()
    sleep(4)
    element = WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.ID, 'btnPagar')))

    #Clicar Botão Segunda Via
    segunda_via = driver.find_element_by_xpath('//*[@class="card-vertical-align ng-scope"]')
    segunda_via.click()
    #sleep(10)

    element = WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.ID, 'debitosPendentes')))
    pdf = driver.find_elements_by_xpath('//*[@class="btn btn-default btn-segunda-via-aberta ng-scope"]')
    driver.execute_script("arguments[0].click();", pdf[0])
    sleep(2)

    element = WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.ID, 'btnVerContaCompleta')))  
    conta_completa = driver.find_element_by_id('btnVerContaCompleta')
    conta_completa.click()
    sleep(20)
    a = a + 1
    print ('Todas faturas Baixadas')
    driver.quit()
  • The element is probably no longer clickable at this point. Try using a try catch

  • how would this Try catch?

  • I don’t understand what 64 is for elifs!! Couldn’t you do it all together?? Put in the code more elifs, chiefly, ninth elif that you said that’s where you’re stopping.

  • all 64 are equal to 9

  • If all are equal, why do they exist??

  • has a list of 64 ids, each Elif takes an id and renames it, all elifs do the same thing, but one for each id, problem that it runs the first 8, on 9 it hangs, but if I put to start straight from 9 it goes, and hangs on 17, would like to understand the pq it hangs from 9 on 9

  • instead of 64 elifs, make a lista_de_ids and use a for id_atual in lista_de_ids: there inside the for you put the code, using this variable id_atual to rename. So you don’t have to repeat the code

  • As for the 9 in 9 lock, it’s probably related to the page. Is the page not the type that has elements that load dynamically as the user scrolls?

  • discovered the error, the page was without invoice and he tried to download it anyway

Show 4 more comments
No answers

Browser other questions tagged

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