有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在selenium中的javaScriptExecutor中发送用户输入

我试图使用javaScriptExecutor在文本框中使用scanner类发送用户输入,但没有获得scanner类捕获的值

    Scanner first_captcha = new Scanner(System.in);
            System.out.println("Please enter the captcha");
            int Captcha = first_captcha.nextInt();

            @SuppressWarnings("unused")
            WebElement captcha = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("captcha")));
            JavascriptExecutor jse = (JavascriptExecutor) driver;
            jse.executeScript("document.getElementById('captcha').value = Captcha");


   /* Value is going to the textbox is "Captcha" but I just wanted to send the value which user provided in the scanner class. */

共 (3) 个答案

  1. # 1 楼答案

    执行字符串没有整型值的占位符。所以你需要重构jse。根据此语法执行脚本调用

    jse.executeScript("document.getElementById('captcha').value = "+Captcha+";");
    
  2. # 2 楼答案

    jse.executeScript("document.getElementById('captcha').value = Captcha");
    

    这里Captcha是您设置的硬编码字符串。必须将Captch作为使用Scanner类获取的变量传递

    使用以下代码:

    JavascriptExecutor jse = (JavascriptExecutor) driver;
    jse.executeScript("document.getElementById('captcha').value = "+Captcha+"");
    
  3. # 3 楼答案

    这应该是有效的:

    jse.executeScript(String.format("document.getElementById('captcha').value = %d", Captcha));