Java WebElement 类型中的方法 sendKeys(CharSequence[]) 不适用于参数 (String)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23485363/
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
The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)
提问by mickey
I am trying to send a String to the sendkeys() method, but it is not accepting and throwing an error as
我正在尝试向 sendkeys() 方法发送一个字符串,但它不接受并抛出错误
my codes follows:
我的代码如下:
package healthcare;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;
public class MailRegister_Webdriver {
public WebDriver driver;
public Selenium selenium;
public void openURL(){
//System.setProperty("webdriver.chrome.driver", "F:\Library\chromedriver.exe");
driver=new FirefoxDriver();
selenium=new WebDriverBackedSelenium(driver, "http://mail.in.com");
driver.get("http://mail.in.com");
}
public void register() throws Exception{
//driver.findElement(By.cssSelector("input.registernow")).click();
selenium.click("css=input.registernow");
Thread.sleep(3000);
driver.findElement(By.id("fname")).sendKeys("Nagesh");
selenium.select("day", "10");
selenium.select("month", "Jun");
new Select(driver.findElement(By.id("year"))).selectByVisibleText("1999");
Thread.sleep(1000);
driver.findElement(By.xpath("(//input[@name='radiousername'])[5]")).click();
Thread.sleep(2000);
driver.findElement(By.id("password")).sendKeys("nag123");
driver.findElement(By.id("repassword")).sendKeys);
driver.findElement(By.id("altemail")).sendKeys();
driver.findElement(By.id("mobileno")).sendKeys("7894561230");
driver.findElement(By.id("imageField")).click();
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
MailRegister_Webdriver m=new MailRegister_Webdriver();
m.openURL();
m.register();
}
}
Can somebody help on this, Why Sendkeys() method is not taking String values as arguments?
有人可以帮忙解决这个问题,为什么 Sendkeys() 方法不将 String 值作为参数?
回答by Gyorgy.Hegedus
Try to click into the WebElement before you sending keys to it:
在向 WebElement 发送密钥之前尝试单击它:
public static void login(WebDriver driver, String userName, String password) {
driver.get("loginPage.html");
Thread.sleep(3000);
driver.findElement(By.id("username")).click();
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys(userName);
Thread.sleep(TestConfiguration.time);
driver.findElement(By.id("password")).click();
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys(password);
Thread.sleep(3000);
driver.findElement(By.name("login")).click();
Thread.sleep(3000);
}
You should use clear() method to clear the input field before using sendKeys().
在使用 sendKeys() 之前,您应该使用 clear() 方法清除输入字段。
回答by Abhishek Yadav
You can try by replacing your following lines of code:
您可以尝试替换以下代码行:
driver.findElement(By.id("password")).sendKeys("nag123");
driver.findElement(By.id("repassword")).sendKeys);
driver.findElement(By.id("altemail")).sendKeys();
driver.findElement(By.id("mobileno")).sendKeys("7894561230");
driver.findElement(By.id("imageField")).click();
to
到
driver.findElement(By.id("password")).sendKeys("nag123");
driver.findElement(By.id("repassword")).sendKeys("");
driver.findElement(By.id("altemail")).sendKeys("");
driver.findElement(By.id("mobileno")).sendKeys("7894561230");
driver.findElement(By.id("imageField")).click();
回答by summer
element.sendKeys(new String[]{"Hello, selenium"});
My code looks like this, it's working.
我的代码看起来像这样,它正在工作。
回答by Venkatesh Pothula
It has a simple solution. Change your compiler compliance level from 1.4 to 1.7.
它有一个简单的解决方案。将您的编译器合规性级别从 1.4 更改为 1.7。
Follow these steps in your eclipse:
在您的日食中执行以下步骤:
- Right click on your java project and select Build Path-> Click on
Configure Build Path... - In project properties window, Click/select Java Compilerat the left
panel - At the right panel, change the Compiler compliance level from 1.4 to 1.7
(Select which is higher version in your eclipse) - Lastly Click on Applyand OK
- 右键单击您的 java 项目并选择Build Path-> 单击
Configure Build Path... - 在项目属性窗口中,单击/选择左侧
面板中的Java Compiler
- 在右侧面板中,将编译器合规性级别从 1.4 更改为 1.7
(在您的 eclipse 中选择哪个版本更高) - 最后点击应用和确定
Now check your code. it will never show the same error.
现在检查您的代码。它永远不会显示相同的错误。
回答by Ripon Al Wasim
Set the JRE System Library again. If you use eclipse follow the steps below:
再次设置 JRE 系统库。如果您使用 eclipse,请按照以下步骤操作:
- Go to project properties
- Select Java Build Pathat the left panel -> Select Librariestab at the right
- Click/select JRE System Library[] -> Click Editbutton at the right side
- Set your preferred JRE and click Finishbutton
- Lastly click OKbutton from the project properties pop up window
- 转到项目属性
- 在左侧面板中选择Java Build Path->在右侧选择Libraries选项卡
- 单击/选择 JRE System Library[] -> 单击右侧的Edit按钮
- 设置您的首选 JRE 并单击“完成”按钮
- 最后从项目属性弹出窗口中单击确定按钮
Instead of editing you can also do by deleting and adding. The steps are:
除了编辑,您还可以通过删除和添加来完成。步骤是:
- Right-click on project ? Properties ? Java Build Path
- Select Libraries tab
- Find the JRE System Library and remove it
- Click Add Library... button at right side ? Add the JRE System Library (Workspace default JRE)
- 右键单击项目 ? 特性 ?Java 构建路径
- 选择库选项卡
- 找到 JRE 系统库并将其删除
- 单击右侧的 Add Library... 按钮?添加 JRE 系统库(工作区默认 JRE)
回答by zevij
Depending on the version of java you need to either convert the primitive (i.e. Char) to String (look here: http://tech.deepumohan.com/2013/03/java-how-to-convert-primitive-char-to.html)
根据 Java 的版本,您需要将原始(即 Char)转换为字符串(请看这里:http: //tech.deepumohan.com/2013/03/java-how-to-convert-primitive-char-to .html)
Or switch to a java version that would do it for you (see here: http://java-performance.info/changes-to-string-java-1-7-0_06/)
或者切换到可以为您完成的 Java 版本(请参见此处:http: //java-performance.info/changes-to-string-java-1-7-0_06/)
回答by Mukesh otwani
There are two possible solution for this
有两种可能的解决方案
1- Change the compiler version from old version to 1.5 or greater.
1- 将编译器版本从旧版本更改为 1.5 或更高版本。
2- Change the JRE version from JRE8 to JRE7.
2- 将 JRE 版本从 JRE8 更改为 JRE7。
I have created a detailed article on this may be it will help.
我已经创建了一篇关于此的详细文章可能会有所帮助。
http://learn-automation.com/solution-for-sendkeyscharsequence-in-selenium/
http://learn-automation.com/solution-for-sendkeyscharsequence-in-selenium/