首页 > 解决方案 > 为什么元素不能使用硒进行交互?selenium.common.exceptions.ElementNotInteractableException:消息:元素不可交互

问题描述

我正在尝试使用 selenium 登录此 [网站][1] 并尝试抓取所有可用数据。但是,我在登录帐户时遇到了问题。错误内容如下:

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

通过查看堆栈跟踪,我知道发送登录电子邮件时出现问题..这是完整的堆栈跟踪。

browser.find_element_by_id('username').send_keys('xyz@pqr.com')
  File "F:\technophile\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT,
  File "technophile\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "F:\technophile\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "F:\technophile\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=92.0.4515.159)

这是我的代码:

websites = ['https://www.spacresearch.com/symbol?s=live-deal&sector=&geography=']
    data = []
    for live_deals in websites:
        browser.get(live_deals)
        #time.sleep(1)

        browser.find_element_by_id('username').send_keys('xyz@pqr.com')
        browser.find_element_by_id('password').send_keys('abc@123%$')
        browser.find_element_by_xpath('/html/body/div[1]/div[1]/form/button').send_keys(Keys.ENTER)

        time.sleep(2)
        #rest of the code

我已经遇到过几次这个错误,我已经使用 webriverWait 解决了它。但是这一次,它没有解决问题,所以我也尝试引入睡眠时间。但这也不起作用。我试过这样的事情:

        # wait =WebDriverWait(browser, 10)
        # wait.until(EC.visibility_of_element_located((By.ID, 'username')))
        # wait.until(EC.visibility_of_element_located((By.ID, 'password')))

错误的具体原因是什么?请告诉我!非常感谢您提前提供的帮助。[1]:https ://www.spacresearch.com/symbol?s=li​​ve-deal§or=&geography=

标签: pythonseleniumselenium-webdriver

解决方案


您的代码不起作用的原因是用户名、密码 ID 不是唯一的。

代码 :-

websites = ['https://www.spacresearch.com/symbol?s=live-deal&sector=&geography=']
    data = []
    for live_deals in websites:
        browser.maximize_window()
        browser.implicitly_wait(30)
        browser.get(live_deals)
        #time.sleep(1)
        wait = WebDriverWait(browser, 10)

        wait.until(EC.element_to_be_clickable((By.XPATH, "(//input[@id='username'])[2]"))).send_keys("xyz@pqr.com")
        wait.until(EC.element_to_be_clickable((By.XPATH, "(//input[@id='password'])[2]"))).send_keys("abc@123%$")
        wait.until(EC.element_to_be_clickable((By.XPATH, "(//button[text()='Next'])[2]"))).click()

        time.sleep(2)
        #rest of the code

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

推荐阅读