【发布时间】:2020-10-15 21:17:49
【问题描述】:
我设置了存储python代码的字符串,并将这个字符串传递给使用sendkey。但是代码变得非结构化,并且在代码中输入了额外的空格和单词。
下面是代码:
//Defining the string
String code = "# Python program to display the Fibonacci sequence\n" +
"\n" +
"def recur_fibo(n):\n" +
" if n <= 1:\n" +
" return n\n" +
" else:\n" +
" return(recur_fibo(n-1) + recur_fibo(n-2))\n" +
"\n" +
"nterms = 10\n" +
"\n" +
"# check if the number of terms is valid\n" +
"if nterms <= 0:\n" +
" print(\"Plese enter a positive integer\")\n" +
"else:\n" +
" print(\"Fibonacci sequence:\")\n" +
" for i in range(nterms):\n" +
" print(recur_fibo(i))";
WebElement elem = driver.findElement(By.xpath("/html/body/app-root/app-test/div[1]/section/div/ace-editor/div[2]/div"));
Actions actions = new Actions(driver);
actions.moveToElement(elem).click().perform();
WebElement elem1 = driver.findElement(By.xpath("/html/body/app-root/app-test/div[1]/section/div/ace-editor/div[2]/div/div[3]"));
Actions actions1 = new Actions(driver);
//Selected all the content of the editor
actions1.moveToElement(elem1).click().click().click().click();
//Entering the code
actions1.moveToElement(elem1).sendKeys(code).perform();
编辑器中输入的代码如下图所示:click to view the screenshot
编辑器中的代码应如下所示:
# Python program to display the Fibonacci sequence
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
在 python 中,间距和缩进非常重要,因此在编辑器中输入的代码在编译时会抛出错误。 有没有办法发送编辑器中的整个代码?
【问题讨论】:
-
使用 selenium 使用示例 textarea 标签尝试了它的工作正常。请参阅 img。请发布您遇到问题的 html dom 代码或站点 url
-
@DecodeD : 你在使用'Ace Editor'吗?
-
@Dilip Meghwal:是的,我的应用程序使用 Ace Editor
-
@Mohamed Sulaimaan 警长:您可以在 Ideone.com 上试用。它不起作用。
-
您的 XPath 定位器肯定需要改进。验证 elem1。此外,actions1.moveToElement(elem1).click().click().click().click() 看起来很可疑。还有一点:为什么要创建两个 Actions 对象?你很可能只需要一个。