Reputation: 23
I'm trying to set text to the following text box-
<input type="text" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="off" spellcheck="false" tabindex="0" aria-label="First name" name="firstName" value="" autocapitalize="sentences" id="firstName" data-initial-value="" badinput="false">
By using the following python code-
import time
import pandas as pd
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
exe_path = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(exe_path)
driver.implicitly_wait(10)
driver.get('https://support.google.com/mail/answer/56256?hl=en')
driver.find_element_by_class_name('action-button').click()
f = driver.find_element_by_id('firstName').send_keys('hfsjdkhf')
It does find the element and the but the cursor just stays there a while and I get the following error-
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="firstName"]"}
(Session info: chrome=80.0.3987.132)
How do I fix this??
Upvotes: 1
Views: 1044
Reputation: 98901
The problem is that after clicking on the action-button
, which opens a new tab, selenium continues active on the old tab, as you can see by using print(driver.current_url)
, the solution is waiting a couple of seconds and then switch to the new tab with driver.switch_to_window(driver.window_handles[1])
, i.e.:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
exe_path = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(exe_path)
wait = WebDriverWait(driver, 10)
driver.get('https://support.google.com/mail/answer/56256?hl=en')
el = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "action-button")))
el.click()
wait.until(EC.number_of_windows_to_be(2)) # wait new tab
driver.switch_to_window(driver.window_handles[1]) # switch to newly opened tab
# now you can send the keys to id firstName
el = wait.until(EC.element_to_be_clickable((By.ID, "firstName")))
el.send_keys('username')
Upvotes: 1
Reputation: 1166
There might be below reasons you are unable to click on web element
Put URl or dom details for more help.
Upvotes: 0