【问题标题】:Entering python code in textarea using sendkey, but not working. Any solution?(selenium Java)使用 sendkey 在 textarea 中输入 python 代码,但不起作用。任何解决方案?(硒Java)
【发布时间】: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 对象?你很可能只需要一个。

标签: java selenium


【解决方案1】:

这是解决方案。

通过在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”变量,我们可以使用 Selenium JavascriptExecutor 设置编辑器文本。

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 Editor 名称,请使用以下方法。

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

【讨论】:

  • @DecodeD :终于找到了解决方案。您必须使用 javaScript 执行器执行以下代码。您可以使用 chrome 开发人员控制台尝试此操作。注意:不要忘记定义代码变量。'var el = document.querySelector("#code-fix-h > div > ace-editor");ace.edit(el).setValue(code);'跨度>
  • 你能分享代码吗,我从来没有使用过javascript执行器,所以有点困惑。我没有在 HTML DOM 中找到 ace.edit() 。我在 Java 中使用 selenium,你在使用 C# 吗?
  • @DecodeD :为您的问题添加了代码,并且工作正常。只需在 URL 中替换有效的身份验证密钥,输入有效的电子邮件 ID 并运行脚本。注意:如果您拥有高级帐户,如果有人使用它可能会花费您一些费用,请从您的第一个评论 URL 中删除身份验证密钥。
猜你喜欢
  • 1970-01-01
  • 2016-08-31
  • 1970-01-01
  • 2011-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
  • 1970-01-01
相关资源
最近更新 更多