簡體   English   中英

selenium.WebElement.sendKeys()出錯

[英]Error with selenium.WebElement.sendKeys()

我正在整理一個小應用程序,使用Java中的Selenium WebDriver在Magento站點上執行自動檢出。 我正在努力學習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();
    }
}

我的getName()行確認我從頁面獲取了我想要的元素。

編譯時我收到此錯誤:

[INFO]編譯失敗/seleniumMagento/src/main/java/com/huuginn/seleniumMagento/App.java:[25,13]在org.openqa.selenium.WebElement中的sendKeys(java.lang.CharSequence ...)不能應用於(java.lang.String)

sendKeys期望一個實現CharSequence的類型的參數(java.lang.String符合條件),所以我不明白為什么我得到這個錯誤。

我正在使用Java 1.6和Selenium 2.19,使用Maven進行構建。

調用sendKeys()遇到了類似的問題。 問題通常是,簽名是一個變量,即CharSequence...而不僅僅是CharSequence

當然這應該不是Java 6的問題。我的猜測是你的maven編譯使用不同的編譯器設置。 無論如何,您可以將代碼更改為

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

幫助診斷問題。

在創建項目時,請確保選擇“使用執行環境JRE:JavaSE-1.6。您可以在沒有任何Sendkeys錯誤的情況下成功執行測試.100%它將起作用。

我發現了解決這個問題的另一種方法。 我沒有指定要編譯的Java版本,因此Maven正在編譯舊版本。 我把它添加到我的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>

這允許我在sendKeys()中只有一個文字字符串“SAMPLE”,它工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM