1

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??

2
  • 1
    Can you try to wait for the element explicitly before trying to send text. The exception says that the element could not be located. driver.find_element_by_id('firstName') looks fine. So please try to wait for the presence of element before calling send_keys Commented Apr 23, 2020 at 21:50
  • The problem is that after clicking on the action-button, which opens a new tab, selenium continues reading on the old tab, as you can see by using print(driver.current_url), please check solution below. Commented Apr 24, 2020 at 1:52

2 Answers 2

1

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')
Sign up to request clarification or add additional context in comments.

3 Comments

If you replace the hardcoded "sleep(2) with "wait.until(EC.number_of_windows_to_be(2))" this would be the best answer.
Thanks Pedro Lobito your correction worked, but is there a way that after clicking the button the result appears on the same page??
@dhruvsangwan.You're welcome. You can change the source code on the first page or the browser default settimgs to force opening ttabs on the same tab: ghacks.net/2009/07/03/force-firefox-to-open-links-in-same-tab/…
0

There might be below reasons you are unable to click on web element

  1. Webelement could be inside frames.So you need to switch to frames and then try to click.
  2. Put explicit wait until element is visible and then try to click.
  3. Might be xpath is not unique for that element make sure element is having unique xpath.

Put URl or dom details for more help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.