java Selenium 显示错误“WebElement 类型中的方法 sendKeys(CharSequence[]) 不适用于参数 (String)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32651792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:33:50  来源:igfitidea点击:

Selenium shows error "The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)"

javaseleniumselenium-webdriverwebdriver

提问by Swapnil Gandhile

While executing selenium script by JAVA, I am getting below error.

通过 JAVA 执行 selenium 脚本时,出现以下错误。

Error : "The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)"

错误:“WebElement 类型中的方法 sendKeys(CharSequence[]) 不适用于参数 (String)”

My code:

我的代码:

package Pkg_09;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class Practice {

    public static void main(String[] args) {

        WebDriver driver1;

        driver1.get("www.google.com");
        driver1.findElement(By.id("gs_htif0")).sendKeys("Sachin Tendulkar");
        driver1.findElement(By.id("aa")).sendKeys("xx");
        driver1.findElement(By.name("btnK")).click();
    }
}

Kindly let me know if there are anything which I am missing out.

如果有什么我遗漏的地方,请告诉我。

回答by Stanislav

This behaviour depends on the Java version you use to run this code. Since version 1.6 it's ok to pass a single String instance as CharSequences which is expected as a parameter by the sendKeys() method. But, if you use an older version or it's specified in your build script as source/target version, then you should pass an Array of CharSequences as a parameter of the sendKeys() method. So it could done like:

此行为取决于您用于运行此代码的 Java 版本。从 1.6 版开始,可以将单个 String 实例作为 CharSequences 传递,它是 sendKeys() 方法期望的参数。但是,如果您使用旧版本或在您的构建脚本中将其指定为源/目标版本,那么您应该传递一个 CharSequences 数组作为 sendKeys() 方法的参数。所以它可以这样做:

public static void main(String[] args) {
    WebDriver driver1;
    driver1.get("www.google.com");
    driver1.findElement(By.id("gs_htif0")).sendKeys(new String[] {"Sachin Tendulkar"});
    driver1.findElement(By.id("aa")).sendKeys(new String[] {"xx"});
    driver1.findElement(By.name("btnK")).click();
}

回答by palki wadhwa

Try Below Code :

尝试以下代码:

driver1.findElement(By.id("aa")).sendKeys(new String[]{"xx"});

回答by user7977023

If anyone still facing this issue(Sendkeys error), please try below.

如果有人仍然面临这个问题(Sendkeys 错误),请尝试以下。

download "cldc-1.1-java5.0.jar" and add it in Build path--> add external libraries.

下载“cldc-1.1-java5.0.jar”并将其添加到构建路径中-->添加外部库。

回答by Shubham Jain

We are using eclipse oxygen version with Java 8 and it's working fine for us. We had face same issue with eclipse Luna and indigo

我们正在使用带有 Java 8 的 Eclipse 氧气版本,它对我们来说运行良好。我们在 eclipse Luna 和 indigo 上遇到了同样的问题

回答by kushal.8

This happens when you try to use JAVA 8 with lower versions of selenium , change your jars to selenium 3.0.1 and everything should work fine.

当您尝试将 JAVA 8 与较低版本的 selenium 一起使用时,会发生这种情况,将您的 jars 更改为 selenium 3.0.1 并且一切正常。