简体   繁体   中英

Error with selenium.WebElement.sendKeys()

I am putting together a small app to perform automated checkouts on a Magento site, using Selenium WebDriver in Java. I'm working on learning Java, so I'm adamant on getting this figured out with Java, and not switching to Ruby or Python.

package com.huuginn.seleniumMagento;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
 * selenium app for completing checkout in magento
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        //      MagentoCatalog c = new MagentoCatalog();
        WebDriver driver = new FirefoxDriver();

        driver.get("http://plmkt.huuginn.com/");

        WebElement searchField = driver.findElement(By.id("search"));

        System.out.println(searchField.getClass().getName());
        searchField.clear();
        searchField.sendKeys("sample");
        searchField.submit();
    }
}

My getName() line confirms that I am getting the element that I want from the page.

I'm getting this error when compiling:

[INFO] Compilation failure /seleniumMagento/src/main/java/com/huuginn/seleniumMagento/App.java:[25,13] sendKeys(java.lang.CharSequence...) in org.openqa.selenium.WebElement cannot be applied to (java.lang.String)

sendKeys is expecting a parameter of a type that implements CharSequence (java.lang.String qualifies as such), so I don't understand why I'm getting this error.

I am using Java 1.6, and Selenium 2.19, doing my build with Maven.

I have had similar problems with calling sendKeys() . The problem usually is, that the signature is a var-ary, that is CharSequence... instead of just CharSequence .

Of course this should not be a problem with Java 6. My guess would be that your maven compile uses a different compiler setting. Anyways you could change your code to

searchField.sendKeys(new String[] { "sample" });

to help diagnose the problem.

在创建项目时,请确保选择“使用执行环境JRE:JavaSE-1.6。您可以在没有任何Sendkeys错误的情况下成功执行测试.100%它将起作用。

I discovered another way to work around this. I was not specifying the version of Java to compile for, so Maven was compiling for an older version. I added this to my pom.xml:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.0.2</version>
    <configuration>
      <source>1.5</source>
      <target>1.5</target>
    </configuration>
  </plugin>

That allows me to just a literal string "SAMPLE" in sendKeys() and it works fine.

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