python - 将文本输入到 codemirror python selenium 的文本区域

标签 python css selenium-chromedriver codemirror

我是 selenium 的新手,正在尝试将文本输入到 CodeMirror 生成的文本区域中。我查看了 textarea 上的其他问题,但无法解决我的问题。

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

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

我意识到添加长源代码可能会给其他用户带来不便,但我不确定哪一行 CodeMirror 可能引用了文本区域,为了完整起见,我将其发布。这是源代码和屏幕截图。

enter image description here

<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()不返回任何东西。如果你想在点击后与元素对象进一步交互,你需要先将它保存到一个变量中。

但更重要的是,您正在单击 <div> 包含 <textarea>而不是 <textarea>本身。即使点击 <div>给出 <textarea>焦点,您的元素对象仍将是包含 <div> 的句柄,它不会对 send_keys() 做出有意义的 react .如果<textarea>是用户输入文本的地方,您必须找到那个元素并与之交互:

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

(除非 <textarea> 被禁用并且需要额外的步骤才能启用它,否则您无需 click() 即可开始输入。)

然后简单地 send_keys()对它:

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

关于python - 将文本输入到 codemirror python selenium 的文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48722343/

相关文章:

python - 多线程访问 Python bytearray

asp.net - css 不适用于两个 id

javascript - FullCalendar.js 调整移动标题

java - 无法使用 Selenium Java 将按键发送到数字输入字段

python - 从 selenium 控制的 chrome 浏览器读取 stdout 和 stderr 日志

python - 创建一种从文件中读取正数和负数的方法

python - 使用正则表达式从字符串中提取数字

python - pyspark 在没有 pandas 的情况下将一列拆分为多列

css - 在 CSS 中为 div 使用多个 ID

java - WebElement在浏览器中使用cssSelector、xpath可定位,虽然不为null,但为NotPresent、NotClickable、NotEnabled