user14417149
user14417149

Reputation:

How to send keys with python selenium after an element has loaded?

My code:

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)

Here I made an attempt of using the WebDriverWait function to allow time for the text box to be loaded before sending the keys.

But it errored and returned this:

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)

The selector ID is correct, being identifierId, but since it isn't loaded properly the code could not find and input text. How should I correct the function used for waiting the element to load in order for this to work?

Upvotes: 0

Views: 3543

Answers (1)

Prophet
Prophet

Reputation: 33361

You are mixing selector with web element.
Here

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

usernameField is web element. While here

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

usernameField should be a string.
Also I'd suggest to prefer using visibility_of_element_located when possible instead of element_to_be_clickable
So, to make your code working try change it as following:

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)

Upvotes: 0

Related Questions