首页 > 解决方案 > 如何在 Python 中使用 selenium 单击文本框并输入文本?

问题描述

我正在尝试使用 Selenium 输入登录页面以转到该页面并单击登录框并输入一些文本。我的代码允许我进入登录页面,单击登录框,这是我的代码中断的部分。

代码

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)

代码打开页面,如果有人想知道或者这可能是什么影响我的代码是登录页面,当您使用访客帐户访问 Google 文档时,单击登录文本框,并且无法输入任何文本. 在代码应该输入文本的时间点,我收到此错误。

错误

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)

诚然,这有点让我头疼,我想知道是否有人可能知道如何解决这个错误,以及他们是如何找到解决方法的,因为我接下来的合理步骤是输入密码,选择新文档,并在 Google Doc 中输入文本。

标签: pythonselenium-webdriver

解决方案


您应该尝试在输入中设置值,但您当前的代码正在获取div元素并且输入框正在拦截对 div 元素的点击。相反,请尝试使用下面的代码行。

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

推荐阅读