简体   繁体   English

找不到使用 Selenium 和 Python 登录 Google 的密码字段

[英]Can't find password field to log into Google using Selenium and Python

So, I'm coding a simple bot that just opens some apps and gives me my work for the day, and when I try to login to Google and I manage to find the username field and fill it, click the button to go to the next page but then, when I try to enter the password I just get a error saying that the password class or xpath(tried both) doesn't exist.所以,我正在编写一个简单的机器人,它只打开一些应用程序并为我提供当天的工作,当我尝试登录 Google 并设法找到用户名字段并填写它时,单击 go 的按钮到下一页但是,当我尝试输入密码时,我只是收到一条错误消息,提示密码 class 或 xpath(都试过)不存在。 I have also tried waiting for the page to load, and still nothing.我也尝试过等待页面加载,但仍然没有。

There's the code.有代码。 I might just be overlooking a simple thing:我可能只是忽略了一件简单的事情:

emailLogin = browser.find_element_by_xpath('//*[@id ="identifierId"]')
emailLogin.send_keys(schoolUser)
browser.find_element_by_class_name("VfPpkd-RLmnJb").click()
passwordLogin = browser.find_element_by_class_name("whsOnd zHQkBf") passwordLogin.send_keys(schoolPassword)

find_element_by_class_name () accept only single class name not multiple classes. find_element_by_class_name () 只接受单个 class 名称而不接受multiple classes. You can use either css selector or xpath to use other attribute values to identify the element.您可以使用css selectorxpath来使用其他属性值来标识元素。

To avoid synchronisation issue use WebDriverWait () and wait for element_to_be_clickable ()为避免同步问题,请使用WebDriverWait () 并等待element_to_be_clickable ()

XPath : XPath

 passwordLogin=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@name='password']")))
 passwordLogin.send_keys(schoolPassword)

Name :姓名

passwordLogin=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"password")))

CSS selector: CSS 选择器:

passwordLogin=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,".whsOnd.zHQkBf[name='password']")))

You need to import below libaries.您需要导入以下库。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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