Capybara、Selenium、Jupyter Notebook:填充代码单元格

Capybara, Selenium, Jupyter Notebook: filling code cell

我想使用 Capybara 将文本输入到 Jupyter Notebook 单元格中。单击该元素并使用 'send_keys' 不起作用,尽管光标最终位于正确的位置:find(".input").click.send_keys("hello")

有没有办法在不选择元素的情况下模拟按键? find("body").send_keys("hello") 也不产生任何文本

编辑:我发现 Jupyter Notebook 使用 CodeMirror。 CodeMirror 以某种方式使用隐藏的 <textarea> 字段,因此这可以解释为什么 Selenium 拒绝找到输入。

我目前的解决方法是找到第一个 CodeMirror 编辑器并在其上使用 setValue 函数:execute_script("var editor = $('.CodeMirror')[0].CodeMirror; editor.setValue('this is the input')")

我通过使用 Selenium 的动作生成器避免了变通方法。找到所需的元素并获取其原生 selenium 表示:

el_native = first('.input').native

然后,使用操作生成器点击元素并发送键:

page.driver.browser.action.click(el_native).send_keys("hello").perform