我设置存储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();
在编辑器中输入的代码如下所示creenshot:click to 查看屏幕截图
编辑器中的代码应如下所示:
# 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中,间距和缩进非常重要,因此在编辑器中输入的代码在编译时会抛出错误。有没有什么方法可以像在编辑器中那样发送整个代码?
1条答案
按热度按时间hjzp0vay1#
这是解决办法。
通过在javascript文件中搜索关键字“ace.edit”找到的编辑器名称。
借助“editor”变量,我们可以使用seleniumjavascriptexecutor设置编辑器文本。
使用完成程序https://ace.c9.io/ 网站。
如果无法识别ace编辑器名称,请使用以下方法。