在通过 JAVA 执行 selenium 脚本时,出现以下错误。
Error : "The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)"
我的代码:
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();
}
}
如果我遗漏了什么,请告诉我。
最佳答案
此行为取决于您用来运行此代码的 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();
}
关于java - Selenium 显示错误 "The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32651792/