使用无头选项时出现 ElementNotInteractableException 错误

Inv*_*tus 2 python selenium

当我尝试运行下面的代码时出现错误。

错误是

ElementNotInteractableException:消息:元素不可交互(会话信息:headless chrome=83.0.4103.116)

这似乎是因为 发生的 chromeOptions.add_argument("--headless")。如果我们使用这个参数,我们如何检查元素?请注意,代码中提供的网站名称是虚拟的。

还有一种方法可以在现有打开的浏览器实例中打开网页,而不是使用新的浏览器实例吗?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time

chromeOptions=Options()
chromeOptions.add_argument("--ignore-certificate-errors")
chromeOptions.add_argument("--incognito")
chromeOptions.add_argument("--headless")

driver = webdriver.Chrome(executable_path=r"C:/Users/lenovo/Downloads/chromedriver_win32/chromedriver.exe",options=chromeOptions)
driver.get("https://abcd.com")
ele=driver.find_element_by_name('q')
time.sleep(10)
ele.clear()
Run Code Online (Sandbox Code Playgroud)

Ric*_*rds 5

尝试设置窗口大小以及无头。添加这个:

chromeOptions.add_argument("--window-size=1920,1080")
Run Code Online (Sandbox Code Playgroud)

无头浏览器的默认大小很小。如果代码在未启用 headless 时有效,可能是因为您的对象位于窗口之外。

[编辑 - 更新了以上不起作用的反馈]

回答你的问题而不是尝试解决你的问题。

将其添加到您的选项中:

--remote-debugging-port=9222
Run Code Online (Sandbox Code Playgroud)

或者在本地运行 chrome(不带 chromedriver):

chrome --headless --remote-debugging-port=9222 --disable-gpu https://www.google.com
Run Code Online (Sandbox Code Playgroud)

然后打开一个新的chrome(或选项卡)并转到http://localhost:9222/

您将获得一个打开页面的链接 - 单击该链接您将获得该远程实例的开发工具: Chrome 远程调试