英文:
Entering python code in textarea using sendkey, but not working. Any solution?(selenium Java)
问题
我将字符串中存储的Python代码设置并传递给sendkey。 但代码变得无结构,额外的空格和单词被输入到代码中。
以下是代码:
//定义字符串
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" +
"# 检查项数是否有效\n" +
"if nterms <= 0:\n" +
" print(\"请输入正整数\")\n" +
"else:\n" +
" print(\"斐波那契数列:\")\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);
//选择编辑器的所有内容
actions1.moveToElement(elem1).click().click().click().click();
//输入代码
actions1.moveToElement(elem1).sendKeys(code).perform();
编辑器中输入的代码如下所示:点击查看截图
编辑器中的代码应如下所示:
# Python程序显示斐波那契数列
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
# 检查项数是否有效
if nterms <= 0:
print("请输入正整数")
else:
print("斐波那契数列:")
for i in range(nterms):
print(recur_fibo(i))
在Python中,空格和缩进非常重要,因此在编辑器中输入的代码会在编译时引发错误。有没有办法将整个代码发送到编辑器中,使其保持原样?
英文:
I set string in which i stored the python code and pass this string to using sendkey. But the code got unstructured and extra spaces and words got entered in the code.
Below is the code:
//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();
The code in Editor entered as shown in below screenshot:click to view the screenshot
The code in editor should be like below:
# 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))
In python spacing and indentation is very important so the code entered in editor throw error on compilation.
Is there any way to send whole code as it's in the editor?
答案1
得分: 0
这是解决方案。
通过在 JavaScript 文件中搜索关键字 "ace.edit" 来找到编辑器的名称。
例如:var editor = ace.edit("editor_demo") // 'editor_demo' 是元素的 id,例如:
借助 'editor' 变量,我们可以使用 Selenium 的 JavascriptExecutor 设置编辑器文本。
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("editor.setValue(arguments[0])", "我们想要设置到编辑器的字符串");
通过使用 https://ace.c9.io/ 网站完成程序。
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\src\\test\\resources\\executables\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://ace.c9.io/");
String code = "print('Hello World')";
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"ace_editor_demo\"]")));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("editor.setValue(arguments[0])", code);
如果无法识别 Ace 编辑器的名称,请使用以下方法。
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\src\\test\\resources\\executables\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String code = "var temp = 'Hello World'";
driver.get("https://example.com/AuthenticateKey?id=ValidAuthKey");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//app-landing-page//button[@type='submit']")));
driver.findElement(By.xpath("//app-landing-page//button[@type='submit']")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//app-authenticate//input[@placeholder='Enter full name']")));
driver.findElement(By.xpath("//app-authenticate//input[@placeholder='Enter full name']")).sendKeys("Test Automation");
driver.findElement(By.xpath("//app-authenticate//input[@placeholder='Enter email address']")).sendKeys("---Your email ID---");
driver.findElement(By.xpath("//app-authenticate//button[@type='submit']")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//app-test-instructions//button[contains(text(), 'Start Test')]")));
driver.findElement(By.xpath("//app-test-instructions//button[contains(text(), 'Start Test')]")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='sectionInstModal']//a[contains(text(), 'Start Section')]")));
driver.findElement(By.xpath("//*[@id='sectionInstModal']//a[contains(text(), 'Start Section')]")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='_sectionTime']")));
JavascriptExecutor js = (JavascriptExecutor)driver;
String jsQuery = "var el = document.querySelector('#code-fix-h > div > ace-editor');ace.edit(el).setValue(arguments[0]);";
js.executeScript(jsQuery, code);
英文:
Here is the solution.
The editor name to be found by searching the keyword "ace.edit" in the javaScript file.
Eg. var editor = ace.edit("editor_demo) // 'editor_demo' is the id of the element Eg. <div id = 'editor_demo'></div>
With help of 'editor' variable we can set the editor text by using Selenium JavascriptExecutor.
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("editor.setValue(arguments[0])", "Desired String that we want to set to the editor");
Complete program by using https://ace.c9.io/ website.
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\src\\test\\resources\\executables\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://ace.c9.io/");
String code = "print('Hello World')";
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"ace_editor_demo\"]")));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("editor.setValue(arguments[0])", code);
If you are not able to identify the Ace Editor name then use below approach.
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\src\\test\\resources\\executables\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String code = "var temp = 'Hello World'";
driver.get("https://example.com/AuthenticateKey?id=ValidAuthKey");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//app-landing-page//button[@type='submit']")));
driver.findElement(By.xpath("//app-landing-page//button[@type='submit']")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//app-authenticate//input[@placeholder='Enter full name']")));
driver.findElement(By.xpath("//app-authenticate//input[@placeholder='Enter full name']")).sendKeys("Test Automation");
driver.findElement(By.xpath("//app-authenticate//input[@placeholder='Enter email address']")).sendKeys("---Your email ID---");
driver.findElement(By.xpath("//app-authenticate//button[@type='submit']")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//app-test-instructions//button[contains(text(), 'Start Test')]")));
driver.findElement(By.xpath("//app-test-instructions//button[contains(text(), 'Start Test')]")).click();;
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='sectionInstModal']//a[contains(text(), 'Start Section')]")));
driver.findElement(By.xpath("//*[@id='sectionInstModal']//a[contains(text(), 'Start Section')]")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='_sectionTime']")));
JavascriptExecutor js = (JavascriptExecutor)driver;
String jsQuery = "var el = document.querySelector('#code-fix-h > div > ace-editor');ace.edit(el).setValue(arguments[0]);";
js.executeScript(jsQuery, code);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论