找不到使用 Selenium 和 Python 登录 Google 的密码字段
Can't find password field to log into Google using Selenium and Python
所以,我正在编写一个简单的机器人程序,它只打开一些应用程序并为我提供当天的工作,当我尝试登录 Google 时,我设法找到用户名字段并填写它,单击按钮转到下一页但是,当我尝试输入密码时,我只是收到一条错误消息,提示密码 class 或 xpath(均已尝试)不存在。
我也试过等待页面加载,但仍然没有。
这是代码。我可能只是忽略了一件简单的事情:
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
() 只接受 单个 class 名称 不 multiple classes.
您可以使用 css selector
或 xpath
来使用其他属性值来标识元素。
为避免同步问题,请使用 WebDriverWait
() 并等待 element_to_be_clickable
()
XPath:
passwordLogin=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@name='password']")))
passwordLogin.send_keys(schoolPassword)
姓名:
passwordLogin=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"password")))
CSS 选择器:
passwordLogin=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,".whsOnd.zHQkBf[name='password']")))
您需要导入以下库。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
所以,我正在编写一个简单的机器人程序,它只打开一些应用程序并为我提供当天的工作,当我尝试登录 Google 时,我设法找到用户名字段并填写它,单击按钮转到下一页但是,当我尝试输入密码时,我只是收到一条错误消息,提示密码 class 或 xpath(均已尝试)不存在。 我也试过等待页面加载,但仍然没有。
这是代码。我可能只是忽略了一件简单的事情:
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
() 只接受 单个 class 名称 不 multiple classes.
您可以使用 css selector
或 xpath
来使用其他属性值来标识元素。
为避免同步问题,请使用 WebDriverWait
() 并等待 element_to_be_clickable
()
XPath:
passwordLogin=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@name='password']")))
passwordLogin.send_keys(schoolPassword)
姓名:
passwordLogin=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"password")))
CSS 选择器:
passwordLogin=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,".whsOnd.zHQkBf[name='password']")))
您需要导入以下库。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC