11

I am trying to send a String to the sendkeys() method, but it is not accepting and throwing an error as

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?

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
mickey
  • 190
  • 1
  • 2
  • 9
  • 1
    Can you let us know @ which line you are facing issue? – nitin chawda May 06 '14 at 04:46
  • It would be great to have stacktrace (or name of exception) and probably snippet of HTML. – olyv May 06 '14 at 05:42
  • driver.findElement(By.id("password")).sendKeys("nag123"); – mickey May 06 '14 at 07:35
  • a red color underline is showing for sendkeys text, and if we analyse the error, The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String) is displaying – mickey May 06 '14 at 07:36
  • Can you please clarify two next strings `driver.findElement(By.id("repassword")).sendKeys);` -- sendKeys without argument and extra bracket `driver.findElement(By.id("altemail")).sendKeys();` -- you use sendKeys without argument, I'm not sure it is valid use. – olyv May 06 '14 at 07:52
  • 1
    Aren't you missing a bracket in `driver.findElement(By.id("repassword")).sendKeys);`? – barak manos May 06 '14 at 10:12
  • driver.findElement(By.id("password")).sendKeys("nag123"); I am asking about this line – mickey May 06 '14 at 18:47
  • What IDE did you use? Eclipse or IntelliJIDEA? – Ripon Al Wasim Jan 21 '15 at 09:13
  • What IDE are you using? Eclipse, IntelliJ IDEA or NetBeans? – Ripon Al Wasim Jan 07 '16 at 12:20

7 Answers7

34

It has a simple solution. Change your compiler compliance level from 1.4 to 1.7.

Follow these steps in your eclipse:

  1. Right click on your java project and select Build Path -> Click on
    Configure Build Path...
  2. In project properties window, Click/select Java Compiler at the left
    panel
  3. At the right panel, change the Compiler compliance level from 1.4 to 1.7
    (Select which is higher version in your eclipse)
  4. Lastly Click on Apply and OK

Now check your code. it will never show the same error.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
  • In JDK Compliance panel: the combo containing 1.4 is showing disabled in my case. So I could not change it to 1.7 or higher version from 1.4 – Ripon Al Wasim Jan 07 '16 at 12:14
  • u have to enable the project specification settings , which is available on java compiler window on top.. then you can change the versions, – Venkatesh Pothula Dec 24 '19 at 10:51
3
element.sendKeys(new String[]{"Hello, selenium"});

My code looks like this, it's working.

Grzegorz Adam Kowalski
  • 5,243
  • 3
  • 29
  • 40
summer
  • 39
  • 1
2

There are two possible solution for this

1- Change the compiler version from old version to 1.5 or greater.

2- Change the JRE version from JRE8 to JRE7.

I have created a detailed article on this may be it will help.

http://learn-automation.com/solution-for-sendkeyscharsequence-in-selenium/

Mukesh otwani
  • 821
  • 8
  • 11
0

Try to click into the WebElement before you sending keys to it:

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().

Gyorgy.Hegedus
  • 361
  • 1
  • 3
  • There is no text previously in the edit box, and the error is coming while writing the code itself – mickey May 06 '14 at 07:38
0

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();
Abhishek Yadav
  • 459
  • 5
  • 16
0

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)

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/)

zevij
  • 2,416
  • 1
  • 23
  • 32
-1

Set the JRE System Library again. If you use eclipse follow the steps below:

  1. Go to project properties
  2. Select Java Build Path at the left panel -> Select Libraries tab at the right
  3. Click/select JRE System Library[] -> Click Edit button at the right side
  4. Set your preferred JRE and click Finish button
  5. Lastly click OK button from the project properties pop up window

Instead of editing you can also do by deleting and adding. The steps are:

  1. Right-click on project » Properties » Java Build Path
  2. Select Libraries tab
  3. Find the JRE System Library and remove it
  4. Click Add Library... button at right side » Add the JRE System Library (Workspace default JRE)
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176