如何使用selenium web驱动程序在代码镜像中输入值?

时间:2016-06-22 11:45:59

标签: javascript java selenium selenium-webdriver webdriver

我目前正面临与代码镜像中的输入值相关的问题(FYI:输入是html代码)

任何帮助都非常感谢!!

这就是我到目前为止所做的事情(但我需要在每行代码镜像中插入值)

JavascriptExecutor js = (JavascriptExecutor)driver;
Js.executeScript("arguments[0].CodeMirror.setValue(\"" + value + "\");", driver.findElementBy(By.id("id")));

enter image description here

页面源代码是:

<div class="CodeMirror cm-s-default">
<div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5px; left: 35px;">
<textarea wrap="off" style="position: absolute; padding: 0px; width: 1px; height: 1em; outline: medium none;" autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0"></textarea>
</div>
<div class="CodeMirror-vscrollbar" cm-not-content="true" style="min-width: 18px;">
<div style="min-width: 1px; height: 0px;"></div>
</div>
<div class="CodeMirror-hscrollbar" cm-not-content="true" style="min-height: 18px;">
<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" draggable="true">
<div class="CodeMirror-sizer" style="margin-left: 30px; margin-bottom: 0px; border-right-width: 30px; min-height: 31px; min-width: 7px; padding-right: 0px; padding-bottom: 0px;">
<div style="position: relative; top: 0px;">
<div class="CodeMirror-lines">
<div style="position: relative; outline: medium none;">
<div class="CodeMirror-measure">
<span>
<span>​</span>
x
</span>
</div>
<div class="CodeMirror-measure"></div>
<div style="position: relative; z-index: 1;"></div>
<div class="CodeMirror-cursors">
<div class="CodeMirror-cursor" style="left: 4px; top: 0px; height: 22.85px;"> </div>
</div>
<div class="CodeMirror-code">
<div style="position: relative;">
<div class="CodeMirror-gutter-wrapper" style="left: -30px;">
<div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 21px;">1</div>
</div>
<pre class=" CodeMirror-line ">
<span>
<span cm-text="">​</span>
</span>
</pre>
</div>
</div>
</div>
</div>
</div>
</div>

所以问题是如何在代码镜像中的不同行中输入值。

2 个答案:

答案 0 :(得分:1)

你的问题不是很清楚,但我认为你要找的是将文字插入不同的行,而不是全部插入一行。如果是这种情况,则需要在字符串中插入\n,以便回车。

一个示例字符串是string sample = "<html>\n <test>\n</html>",应生成

<html>
  <test>
</html>

答案 1 :(得分:0)

我可以通过将输入值更改为此来输入带换行符的代码镜像。 <html>\\r<test>\\r</html>