Home > Enterprise >  Website login loading endlessly when I use find_element()
Website login loading endlessly when I use find_element()

Time:07-05

I'm having a problem with Selenium, when fetching an input from a login from a page. For some reason, whenever I store the element in a variable, the site in question, when trying to log in, keeps loading infinitely. However, if I remove that part of the code, and enter the login credentials manually, the login is done normally. I will explain below:

This is how the page input I try to access looks like:

<input id="txtUsername" name="txtUsername"  placeholder="User" type="text" required="required" aria-required="true" autocomplete="off" autocorrect="off" autocapitalize="off">

<input id="txtPassword" name="txtUsername"  placeholder="User" type="text" required="required" aria-required="true" autocomplete="off" autocorrect="off" autocapitalize="off">

And this is my python code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import chrome
from time import sleep


options = webdriver.ChromeOptions()
browser = webdriver.Chrome(options=options)
site = "www.google.com"
browser.get(site)


def make_login(browser):
    sleep(1)
    login = browser.find_element(By.ID, "txtUsername")
    login.click()
    login_text = "User"
    for x in login_text:
        sleep(0.15)
        login.send_keys(x)
    senha = navegador.find_element(By.ID, "txtPassword")
    senha.click()
    senha_text = "Password"
    for x in senha_text:
        sleep(0.15)
        senha.send_keys(x)

if __name__ == "__main__":
    make_login(browser)

When I run it, it clicks on each input, enters the password, as it should. However, when I click log in, the website keeps loading endlessly.

If i remove this part:

login = browser.find_element(By.ID, "txtUsername")
login.click()

senha = navegador.find_element(By.ID, "txtPassword")
senha.click()

And manually clicking on the inputs, he enters the site normally... I appreciate anyone who can help me.

CodePudding user response:

Have you tried to use only use send_keys()? So, your code will look like:

def make_login(browser):
    sleep(1)
    login = browser.find_element(By.ID, "txtUsername")
    login_text = "User"
    login.send_keys(login_text)
    senha = navegador.find_element(By.ID, "txtPassword")
    senha_text = "Password"
    senha.send_keys(senha_text)

As shown in selenium documentation send_keys() method is enough to fill input fields.

But if you're trying to access sites like bet365 it's almost certainly the website prohibiting your code. Take a look at this post and its answers.

  • Related