简体   繁体   中英

Java with Selenium WebDriver

I am trying to automate gmail login.

When I enter the text in username input box using sendKeys() it is throwing an exception.

My code:

WebElement userName =   driver.findElement(By.id("Email"));
userName.sendKeys("tutorial");

Exception:

Error:The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String) at com.gmail.test.Gmaillogin.main(Gmaillogin.java:65)

It's tell you that the sendKeys method get only CharSequence[] type. You have to create CharSequence[] and insert to it your value and use it within sendKeys method.

See this for using CharSequence: How to convert a String to CharSequence?

Thank you guys for helping me out. I am able to solve above problem.

Code that worked: userName.sendKeys(new String[]{"tutorial"});

For further details please see this link : Error when using sendKeys() with Selenium WebDriver Java.lang.CharSequence cannot be resolved

Just check and update the Project language level to SDK Default(IntelliJ, not sure about eclipse) under your Project settings, it worked for me.

Thanks

SendKeys method input should be CharacterSequence array but not String. But in Java, String is equal to a CharSequence. So you can do as following way

WebElement userName =   driver.findElement(By.id("Email"));
CharacterSequence[] cs = new String[]{"tutorial"};
userName.sendKeys(cs);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM