简体   繁体   中英

Python selenium issue: able to find element but not send_keys

I am trying to login into the ESPN footytips website so that I can scrape information for one of my leagues.

I am having no issues opening an instance of Chrome and navigating to the homepage (which contains the login form) and can even select the username field but I cannot for the life of me send my login details to the form.

In debugging I know I can find and select the form submit button and the issue seems to be in passing my login details using send_keys as my exception rule always triggers after I attempt call send_keys.

Any suggestions on how to resolve would be welcomed! My script is below:

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys

    login_address = "http://www.footytips.com.au/home"
    me_login = "test@test.com"
    me_password = "N0TMYR3@LP@S5W0RD"

    browser = webdriver.Chrome()
    browser.get(login_address)

    try:
        login_field = browser.find_element_by_id("ft_username")
        password_field = browser.find_element_by_id("ft_password")
        print("User login fields found")

        login_field.send_keys(me_login)
        password_field.send_keys(me_password)
        print("Entered login data")

        submit_button = browser.find_element_by_id("signin-ft")
        print("Submit button found")
        submit_button.submit()

    except:
        print("Error: unable to enter form data")

The locators you have used doesn't uniquely identifies the login_field and the password_field . Additionally you need to wait for the respective WebElements to be visible. Here is your own code with some tweaks :

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#lines of code
login_address = "http://www.footytips.com.au/home"
me_login = "test@test.com"
me_password = "N0TMYR3@LP@S5W0RD"
browser.get(login_address)
login_field = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='login-form']//input[@id='ft_username']")))
password_field = browser.find_element_by_xpath("//div[@class='login-form']//input[@id='ft_password']")
login_field.send_keys(me_login)
password_field.send_keys(me_password)
print("Entered login data")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM