简体   繁体   中英

How to click on a text box and type in text using selenium in Python?

I am attempting to try and type into a login page using Selenium to go to the page and click on the login box and type in some text. My code allows me to go to the login page, click the login box, and this is the part where my code breaks.

Code

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
driver = webdriver.Chrome(executable_path = r'C:\Users\user\Downloads\chromedriver_win32\chromedriver.exe')
driver.get("https://accounts.google.com/signin/v2/identifier?passive=1209600&continue=https%3A%2F%2Fdocs.google.com%2F&followup=https%3A%2F%2Fdocs.google.com%2F&emr=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin")
text_area= driver.find_element_by_xpath('//*[@id="view_container"]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[2]')
text_area.click()
text_area.send_keys(email_address)

The code opens the page, which in case anyone is wondering or if this might be what is affecting my code is the login page when you go to Google Docs on a guest account, clicks on the login text box, and fails to type any text. At the point in time that the code should be typing in text, I get this error.

Error

Traceback (most recent call last):
  File "file.py", line 8, in <module>
    text_area.click()
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <div class="i9lrp mIZh1c"></div> is not clickable at point (508, 242). Other element would receive the click: <input type="email" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="username" spellcheck="false" tabindex="0" aria-label="Email or phone" name="identifier" value="" autocapitalize="none" id="identifierId" dir="ltr" data-initial-dir="ltr" data-initial-value="">
  (Session info: chrome=81.0.4044.92)

Admittedly this is a bit over my head and I am wondering if anyone might know how to fix this error, and how they came to find out how to do so since my next reasonable steps would be to type in the password, select new document, and type text into the Google Doc.

You should try to set the value in input but instead your current code is getting div element and inputbox is intercepting the click on the div element. Instead please, try with the below line of code.

text_area= driver.find_element_by_xpath("//input[@name='identifier']")

You can use waits , ie:

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.keys import Keys

driver = webdriver.Chrome(executable_path="C:\Users\user\Downloads\chromedriver_win32\chromedriver.exe")
driver.maximize_window()
driver.get("https://accounts.google.com/signin/v2/identifier?passive=1209600&continue=https%3A%2F%2Fdocs.google.com%2F&followup=https%3A%2F%2Fdocs.google.com%2F&emr=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin")
wait = WebDriverWait(driver, 10)

# usr
el = wait.until(EC.visibility_of_element_located((By.ID, "identifierId")))
el.send_keys("username@gmail.com")
el.send_keys(Keys.ENTER)

# pwd
el = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@type='password']")))
el.send_keys("XXXXX")
el.send_keys(Keys.ENTER)

# you're logged

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM