使用sendkey在textarea中输入python代码,但不起作用任何解决方案?(selenium java)

niwlg2el  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(341)

我设置存储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中,间距和缩进非常重要,因此在编辑器中输入的代码在编译时会抛出错误。有没有什么方法可以像在编辑器中那样发送整个代码?

hjzp0vay

hjzp0vay1#

这是解决办法。
通过在javascript文件中搜索关键字“ace.edit”找到的编辑器名称。

Eg. var editor = ace.edit("editor_demo) // 'editor_demo' is the id of the element Eg. <div id = 'editor_demo'></div>

借助“editor”变量,我们可以使用seleniumjavascriptexecutor设置编辑器文本。

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("editor.setValue(arguments[0])", "Desired String that we want to set to the editor");

使用完成程序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);

相关问题