[英]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. 我正在尝试登录ESPN footytips网站,以便我可以检索某个联赛的信息。
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. 我没有打开Chrome实例并浏览到主页(包含登录表单)的问题,甚至可以选择用户名字段,但是我一生无法将登录详细信息发送到表单。
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. 在调试中,我知道可以找到并选择表单提交按钮,问题似乎出在使用send_keys传递我的登录详细信息的过程中,因为异常规则总是在尝试调用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
. 您使用的定位器不会唯一地标识login_field
和password_field
。 Additionally you need to wait for the respective WebElements
to be visible. 此外,您需要等待各个WebElements
可见。 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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.