Selenium错误,即“ selenium.common.exceptions.ElementNotInteractableException:消息:元素不可交互”

问题描述

在尝试自动在whatsapp中发送消息时收到此错误。这是源代码

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

from selenium import webdriver
from selenium.webdriver.support.ui import webdriverwait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome('C:/Users/HP/Downloads/chromedriver/chromedriver.exe')
driver.get("https://web.whatsapp.com/")
wait = webdriverwait(driver,600)
target = 'Name of friend or group'
string = "Hello!! sent using selenium.."
x_arg = '//span[contains(@title,' + target + ')]'
group_title = wait.until(EC.presence_of_element_located((By.XPATH,x_arg)))
group_title.click()
inp_xpath = '//div[@class="input"][@dir="auto"][@data-tab="1"]'
input_Box = wait.until(EC.presence_of_element_located((By.XPATH,inp_xpath)))
for i in range(100):
    input_Box.send_keys(string + Keys.ENTER)
    time.sleep(1)

解决方法

当您尝试与之交互的元素不在当前服务器场的主流中时,会发生这种情况 您应该考虑更改框架:

driver.switch_to.frame(input_box)

完成后

driver.switch_to.default_content()
,

selenium.common.exceptions.ElementNotInteractableException:消息:元素不可交互此错误的意思是,当您尝试单击某个元素时,它是不可交互的。因此,例如,如果您首先删除了按钮列表,则应打开此列表,然后单击按钮。可能是您执行此代码group_title.click()时您的group_title无法交互。