【问题标题】:Element not interactable (Input field) when sending keys发送键时元素不可交互(输入字段)
【发布时间】:2021-04-26 22:33:17
【问题描述】:

我正在尝试使用 Selenium 登录该网站: https://www.gamingintelligence.com/my-account

到目前为止我已经这样做了:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

username = 'my_user'
password = 'my_pass'

chrome_options = Options()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='./chromedriver')

url = "https://www.gamingintelligence.com/my-account"

driver.get(url)
driver.find_element_by_xpath('//*[@id="nav-login-tab"]').click()
driver.find_element_by_xpath('//*[@id="username"]').send_keys(username)

但我收到此错误:

ElementNotInteractableException: Message: element not interactable

HTML 是:

<input type="text" class="form-control xh-highlight" name="username" 
id="username" autocomplete="username" value="" required="" style="background- 
image: ...">

【问题讨论】:

    标签: python selenium authentication input


    【解决方案1】:

    要准确解决此问题,您需要添加显式等待:

    import time
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    username = 'my_user'
    password = 'my_pass'
    
    chrome_options = Options()
    chrome_options.add_argument("--incognito")
    driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='/snap/bin/chromium.chromedriver')
    
    url = "https://www.gamingintelligence.com/my-account"
    
    driver.get(url)
    wait = WebDriverWait(driver, 20)
    wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="nav-login-tab"]')))
    driver.find_element_by_xpath('//*[@id="nav-login-tab"]').click()
    field = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="username"]')))
    driver.find_element_by_xpath('//*[@id="username"]').send_keys(username)
    

    或者,您可以在driver.get() 之后设置隐式等待:

    driver.implicitly_wait(15)
    

    第一种方式更可靠。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 2021-08-29
      • 2019-06-20
      • 2020-04-21
      • 2021-10-20
      • 1970-01-01
      • 2021-03-09
      相关资源
      最近更新 更多