将文本输入到textarea中以用于Codemirror python硒 [英] Input text into textarea for codemirror python selenium

查看:165
本文介绍了将文本输入到textarea中以用于Codemirror python硒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是硒的新手,正在尝试将文本输入到CodeMirror生成的文本区域中。我看过有关textarea的其他问题,无法解决我的问题。

I am new to selenium and trying to input text into a textarea generated by CodeMirror. I have looked at the other questions on textarea and am unable to solve my problem.

我正在使用Chrome,并且在textarea所在的资源中找到并能够单击它使光标闪烁。但是,我发现它没有属性。如何在文本区域输入文本?我尝试了其他元素,但遇到了无法聚焦或不可见的错误,我猜这意味着这些元素不是文本区域。

I am using Chrome and have found in sources where the textarea is and am able to click it to have the cursor blinking. However, I find that it has no attributes. How can I input text into the textarea? I have tried other elements and I have gotten either a "can't focus" or "not visible" error which I am guessing means that those elements are not the textarea.

a= browser.find_element_by_css_selector('div.CodeMirror.CodeMirror-wrap.dojoDndTarget.cm-s-sql.dojoDndContainer').click()
print a
None

我意识到添加长代码可能对其他用户不方便,但是我不确定CodeMirror行可能引用了文本区域,并为了完整性而将其发布。这是源代码以及屏幕截图。

I realize that adding a long source code is probably inconvenient for other users but I am not sure which CodeMirror line may refer to the textarea and am posting it for completeness. Here is the source code as well as the screenshot.

<div class="CodeMirror CodeMirror-wrap dojoDndTarget cm-s-sql dojoDndContainer" style="font-size: 12px; font-weight: normal;">
<div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 4px; left: 4px;">
<textarea autocorrect="off" autocapitalize="off" spellcheck="false" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;" tabindex="0">
</textarea>
</div>
<div class="CodeMirror-vscrollbar" not-content="true" style="min-width: 18px;">
<div style="min-width: 1px; height: 0px;">
</div>
</div>
<div class="CodeMirror-hscrollbar" not-content="true" style="min-height: 18px;">
<div style="height: 100%; min-height: 1px; width: 0px;">
</div>
</div>
<div class="CodeMirror-scrollbar-filler" not-content="true">
</div>
<div class="CodeMirror-gutter-filler" not-content="true">
</div>
<div class="CodeMirror-scroll" tabindex="-1">
<div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 30px; min-height: 24px; padding-right: 0px; padding-bottom: 0px;">
<div style="position: relative; top: 0px;">
<div class="CodeMirror-lines">
<div 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="">
<div class="CodeMirror-cursor" style="left: 4px; top: 0px; height: 16px;">&nbsp;</div></div><div class="CodeMirror-code">
<pre class="">
<span style="padding-right: 0.1px;">
<span>
</span>
</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div style="position: absolute; height: 30px; width: 1px; top: 24px;"></div><div class="CodeMirror-gutters" style="display: none; height: 197px;">
</div>
</div>
</div>

谢谢。

推荐答案

在Selenium中, WebElement#click()不返回任何内容。如果要在单击后进一步与element对象进行交互,则需要先将其保存到变量中。

In Selenium, WebElement#click() doesn't return anything. If you want to interact with the element object further after clicking, you'll need to save it to a variable first.

但更重要的是,您单击<包含 < textarea> 而不是<$ c $的code>< div> c>< textarea> 本身。即使单击< div> 给予< textarea> 焦点,您的元素对象仍将是一个句柄包含< div> 上,不会对 send_keys()做出有意义的反应。如果< textarea> 是用户输入文本的位置,则必须找到 that 元素并与之交互:

But more importantly, you're clicking the <div> that contains the <textarea> rather than the <textarea> itself. Even if clicking the <div> gives the <textarea> focus, your element object will still be a handle on the containing <div>, which won't react meaningfully to send_keys(). If the <textarea> is where the user enters text, you must find and interact with that element:

textarea = browser.find_element_by_css_selector('.CodeMirror textarea')

(并且除非< textarea> 被禁用并且需要额外的步骤来启用它,否则您不需要到 click()开始输入。)

(And unless the <textarea> is disabled and requires extra steps to enable it, you don't need to click() it to begin typing in it.)

然后只需 send_keys( )

textarea.send_keys('alert("Hello, World!")')

这篇关于将文本输入到textarea中以用于Codemirror python硒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆