简体   繁体   English

python Selenium send_keys到Jupyter笔记本

[英]python Selenium send_keys to a Jupyter notebook

I am attempting to automate input to a Jupyter notebook with selenium in python. 我正在尝试使用python中的硒自动输入到Jupyter笔记本。

I am attempting to use the send_keys() method with various elements within the DOM and all that I have tried so far have the following issue: 我正在尝试对DOM中的各种元素使用send_keys()方法,到目前为止,我尝试过的所有操作都有以下问题:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <span> is not reachable by keyboard

So far I have attempted locators that return the following elemets: 到目前为止,我已经尝试过返回以下要素的定位器:

  • ('tag name', 'textarea') [However, this is hidden and therefore not interactable] (“标签名称”,“文本区域”)[但是,这是隐藏的,因此不可交互]
  • ('tag name', 'pre') under the ('class', 'CodeMirror-code') element ('class','CodeMirror-code')元素下的('tag name','pre')
  • ('tag name', 'span') under the ('class', 'CodeMirror-code') element ('class','CodeMirror-code')元素下的('tag name','span')
  • ('tag name', 'span') under the span above (“标签名称”,“ span”)

There is no apparent input in the DOM that can be seen. 可以看到DOM中没有明显的input Has anyone attempted to automate a Jupyter notebook? 有没有人尝试自动化Jupyter笔记本?

It appears that when manually typing into the textarea on the screen the DOM is dynamically changing the span elements below... 似乎在屏幕上手动键入文本区域时,DOM正在动态更改下面的span元素...

Here is the relevant part of the DOM I am working with and a screenshot of the textarea 这是我正在使用的DOM的相关部分以及textarea的屏幕截图 输入

<div class="cell code_cell rendered selected" tabindex="2">
   <div class="input">
      <div class="run_this_cell" title="Run this cell"><i class="fa-step-forward fa"></i></div>
      <div class="prompt input_prompt"><bdi>In</bdi>&nbsp;[&nbsp;]:</div>
      <div class="inner_cell">
         <div class="ctb_hideshow">
            <div class="celltoolbar"></div>
         </div>
         <div class="input_area">
            <div class="CodeMirror cm-s-ipython">
               <div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div>
               <div class="CodeMirror-vscrollbar" cm-not-content="true">
                  <div style="min-width: 1px; height: 0px;"></div>
               </div>
               <div class="CodeMirror-hscrollbar" cm-not-content="true">
                  <div style="height: 100%; min-height: 1px; width: 0px;"></div>
               </div>
               <div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div>
               <div class="CodeMirror-gutter-filler" cm-not-content="true"></div>
               <div class="CodeMirror-scroll" tabindex="-1">
                  <div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 8.59375px; margin-bottom: -15px; border-right-width: 15px; min-height: 28px; padding-right: 0px; padding-bottom: 0px;">
                     <div style="position: relative; top: 0px;">
                        <div class="CodeMirror-lines" role="presentation">
                           <div role="presentation" style="position: relative; outline: none;">
                              <div class="CodeMirror-measure"></div>
                              <div class="CodeMirror-measure"></div>
                              <div style="position: relative; z-index: 1;"></div>
                              <div class="CodeMirror-cursors" style="visibility: hidden;">
                                 <div class="CodeMirror-cursor" style="left: 5.59375px; top: 0px; height: 17px;">&nbsp;</div>
                              </div>
                              <div class="CodeMirror-code" role="presentation">
                                 <pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre>
                              </div>
                           </div>
                        </div>
                     </div>
                  </div>
                  <div style="position: absolute; height: 15px; width: 1px; border-bottom: 0px solid transparent; top: 28px;"></div>
                  <div class="CodeMirror-gutters" style="display: none; height: 43px;"></div>
               </div>
            </div>
         </div>
      </div>
   </div>
   <div class="output_wrapper">
      <div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output"></div>
      <div class="output"></div>
      <div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div>
   </div>
</div>

NOTE I have been successful when changing the DOM with javascript execution by making the textarea visible and then sending keys to it. 注意:通过使textarea可见,然后向其发送密钥,可以在通过JavaScript执行更改DOM时取得成功。 (This was done by removing hidden; from the style attribute in the parent div element that it inherited from. However, I am looking for a solution which does not require altering the DOM. (这是通过从其继承的父div元素的style属性中删除hidden;来完成的。但是,我正在寻找一种无需更改DOM的解决方案。

I think the problem is that you're trying to use sendKeys() on an element that is not a text input or textarea. 我认为问题是您试图在不是文本输入或textarea的元素上使用sendKeys()。

Instead try using Actions, after clicking the target element: 在单击目标元素后,尝试使用操作:

Actions builder = new Actions(driver);
builder.SendKeys("My input").Perform();

I tested this on a jsfiddle.net (which doesn't use a standard textarea either) and it did the job. 我在jsfiddle.net(也不使用标准textarea)上进行了测试,并完成了工作。

My example is in c#, the Python documentation for performing action chaining can be found here . 我的示例在c#中,有关执行操作链接的Python文档可在此处找到。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM