简体   繁体   English

selenium.WebElement.sendKeys()出错

[英]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. 我正在整理一个小应用程序,使用Java中的Selenium WebDriver在Magento站点上执行自动检出。 I'm working on learning Java, so I'm adamant on getting this figured out with Java, and not switching to Ruby or Python. 我正在努力学习Java,所以我坚持用Java来解决这个问题,而不是改用Ruby或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. 我的getName()行确认我从页面获取了我想要的元素。

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) [INFO]编译失败/seleniumMagento/src/main/java/com/huuginn/seleniumMagento/App.java:[25,13]在org.openqa.selenium.WebElement中的sendKeys(java.lang.CharSequence ...)不能应用于(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. sendKeys期望一个实现CharSequence的类型的参数(java.lang.String符合条件),所以我不明白为什么我得到这个错误。

I am using Java 1.6, and Selenium 2.19, doing my build with Maven. 我正在使用Java 1.6和Selenium 2.19,使用Maven进行构建。

I have had similar problems with calling sendKeys() . 调用sendKeys()遇到了类似的问题。 The problem usually is, that the signature is a var-ary, that is CharSequence... instead of just CharSequence . 问题通常是,签名是一个变量,即CharSequence...而不仅仅是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. 当然这应该不是Java 6的问题。我的猜测是你的maven编译使用不同的编译器设置。 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. 我没有指定要编译的Java版本,因此Maven正在编译旧版本。 I added this to my pom.xml: 我把它添加到我的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. 这允许我在sendKeys()中只有一个文字字符串“SAMPLE”,它工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sendKeys 方法在 selenium 中引发错误,例如“webelement 类型中的方法 sendKeys(char sequence[] ) 不适用于字符串” - sendKeys method throwing an error in selenium like"The method sendKeys(char sequence[] )in the type webelement is not applicable for the string" Selenium 显示错误“WebElement 类型中的方法 sendKeys(CharSequence[]) 不适用于参数 (String)” - Selenium shows error "The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)" Selenium Webdriver错误“ WebElement类型的方法sendKeys(CharSequence…)引用缺少的CharSequence类型” - Selenium Webdriver error “The method sendKeys(CharSequence…) from the type WebElement refers to the missing type CharSequence” 如何防止Selenium WebElement sendKeys()方法执行参数替换 - How to Prevent Selenium WebElement sendKeys() Method from Performing Parameter Substitution Selenium DoubleClick WebElement导致错误 - Selenium DoubleClick WebElement causing Error Webelement getText方法Selenium WebDriver出现错误 - Getting error for Webelement getText method Selenium WebDriver WebElement.SendKeys(路径)问题:org.openqa.selenium.InvalidArgumentException:无效参数:找不到文件 - Issue with WebElement.SendKeys(path): org.openqa.selenium.InvalidArgumentException: invalid argument: File not found WebElement中的sendKeys(java.lang.CharSequence…)无法应用于(org.openqa.selenium.Keys) - sendKeys (java.lang.CharSequence…) in WebElement cannot be applied to (org.openqa.selenium.Keys) 硒:sendKeys() - Selenium: sendKeys() 找不到Java-Selenium WebElement错误 - Java-Selenium WebElement is not found error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM