Entering python code in textarea using sendkey, but not working. Any solution?(selenium Java)

删除回忆录丶 提交于 2021-02-11 12:55:24

问题


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:


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);


来源:https://stackoverflow.com/questions/63718405/entering-python-code-in-textarea-using-sendkey-but-not-working-any-solutions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!