猿问

如何在 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)


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


阿晨1998
浏览 396回答 2
2回答

狐的传说

您应该尝试在输入中设置值,但您当前的代码正在获取div元素,而输入框正在拦截对 div 元素的点击。相反,请尝试使用下面的代码行。text_area=&nbsp;driver.find_element_by_xpath("//input[@name='identifier']")

RISEBY

您可以使用waits,即:from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.keys import Keysdriver = 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)# usrel = wait.until(EC.visibility_of_element_located((By.ID, "identifierId")))el.send_keys("username@gmail.com")el.send_keys(Keys.ENTER)# pwdel = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@type='password']")))el.send_keys("XXXXX")el.send_keys(Keys.ENTER)# you're logged
随时随地看视频慕课网APP

相关分类

Go
我要回答