簡體   English   中英

如何單擊文本框並使用 Python 中的 selenium 輸入文本?

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

我正在嘗試使用 Selenium 到 go 輸入登錄頁面,然后單擊登錄框並輸入一些文本。 我的代碼允許我 go 到登錄頁面,單擊登錄框,這是我的代碼中斷的部分。

代碼

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)

代碼打開頁面,如果有人想知道或者這可能是什么影響我的代碼是登錄頁面,當您 go 在訪客帳戶上訪問 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)

誠然,這有點超出我的想象,我想知道是否有人可能知道如何解決此錯誤,以及他們是如何找到解決方法的,因為我接下來的合理步驟是輸入密碼 select 新文檔,並在 Google Doc 中輸入文本。

您應該嘗試在輸入中設置值,但您當前的代碼正在獲取div元素,而輸入框正在攔截對 div 元素的點擊。 相反,請嘗試使用下面的代碼行。

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

您可以使用waits ,即:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM