如何在元素加载后使用python selenium发送密钥



我的代码:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as when
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import time; from datetime import datetime
driver = webdriver.Chrome('/home/wit/PycharmProjects/gmeet-automation/chromedriver')
driver.get("https://classroom.google.com")
element = driver.find_element(By.XPATH, '//*[@id="gfe-main-content"]/section[1]/div/div/div/ul/li[2]/a')
element.click()
wait = webdriver.support.ui.WebDriverWait(driver, 5)
usernameField = driver.find_element(By.ID, 'identifierId')
username = wait.until(when.element_to_be_clickable((By.ID, usernameField)))
#username = webdriver.support.ui.WebDriverWait(driver, 5).until(when.element_to_be_clickable((By.ID, driver.find_element(By.ID, 'identifierId'))))
time.sleep(1)
username.send_keys("xxx)

在这里,我尝试使用WebDriverWait函数,以便在发送密钥之前有时间加载文本框。

但它出错了,返回了这个:

File "/home/wit/PycharmProjects/gmeet-automation/main.py", line 20, in <module>
username = driver.find_element(By.ID, 'identifierId')
File "/home/wit/PycharmProjects/gmeet-automation/venv/git/google-meet-project/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 1244, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/home/wit/PycharmProjects/gmeet-automation/venv/git/google-meet-project/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "/home/wit/PycharmProjects/gmeet-automation/venv/git/google-meet-project/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="identifierId"]"}
(Session info: chrome=96.0.4664.110)

选择器ID是正确的,为identifierId,但由于没有正确加载,代码无法找到和输入文本。我应该如何更正用于等待元素加载的函数,以使其正常工作?

您将选择器和web元素混合在一起
此处

usernameField = driver.find_element(By.ID, 'identifierId')

CCD_ 3是web元素。在这里

username = wait.until(when.element_to_be_clickable((By.ID, usernameField)))

usernameField应该是一个字符串
此外,我建议尽可能使用visibility_of_element_located而不是element_to_be_clickable
因此,为了使代码正常工作,请尝试更改如下:

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
from selenium.webdriver.common.by import By
import time; from datetime import datetime
driver = webdriver.Chrome('/home/wit/PycharmProjects/gmeet-automation/chromedriver')
wait = WebDriverWait(driver, 20)
driver.get("https://classroom.google.com")
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="gfe-main-content"]/section[1]/div/div/div/ul/li[2]/a'))).click()
wait.until(EC.visibility_of_element_located((By.ID, "identifierId"))).send_keys("xxx)

相关内容

  • 没有找到相关文章

最新更新