java - Selenium 网络驱动程序 : <WebElement> Iterator cannot be resolved to a type

标签 java eclipse selenium-webdriver automated-tests

我的问题简述:

现在通过我的 selenium 网络驱动程序,我正在测试列表框中的元素列表,在使用以下代码时出现错误

测试配置:

  • 火狐浏览器
  • eclipse 靛蓝
  • Selenium 网络驱动程序 2

我测试的场景:

  1. 打开网站
  2. 选择并显示列表框项目
  3. 写到控制台

我的错误:

java.lang.Error: Unresolved compilation problems: The type List is not generic; it cannot be parameterized with arguments Iterator cannot be resolved to a type

我的代码:

package com.example.tests;

import java.util.Iterator;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.WebElement;
import org.openqa.jetty.html.List;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import com.browsersetup.test.BrowserSetup;
import org.testng.*;
import org.testng.annotations.*;
import org.testng.annotations.Test;

@SuppressWarnings("unused")
public class sample2 extends BrowserSetup{
    private boolean acceptNextAlert = true;
    private StringBuffer verificationErrors = new StringBuffer();

    @BeforeClass
    public void setUp() throws Exception {
        driver = new OperaDriver();
        baseUrl = "http://www.ebay.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        System.out.println("Browser = " + driver);
        System.out.println("Base URL = " + baseUrl);
    }

    @Test
    public void testUntitled() throws Exception {
        driver.get(baseUrl + "/");
        Thread.sleep(10000);
        WebElement element = driver.findElement(By.name("_sacat"));
        Select dd= new Select(element);
        List<WebElement> allOptions= dd.getOptions();

        //To go through the list, we can use an Iterator. 
        //Iterator should be of the same type as the List
        //which is WebElement in this case.

        Iterator<WebElement> it = allOptions.iterator();
        //Using while loop, we can iterate till the List has 
        //a next WebElement [hasNext() is true]
        //number of items in the list
        System.out.println(allOptions.size());

        while(it.hasNext()){
            //When you say it.next(), it points to a particular
            //WebElement in the List.
            WebElement el = it.next();
             //Check for the required element by Text and click it
        if(el.getText().equals("mango")){
            System.out.println(el.getAttribute("value"));
            el.click();
        }
    }

    @AfterClass
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    private boolean isAlertPresent() {
        try {
            driver.switchTo().alert();
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }

    private String closeAlertAndGetItsText() {
        try {
            Alert alert = driver.switchTo().alert();
            String alertText = alert.getText();
            if (acceptNextAlert) {
                alert.accept();
            } else {
                alert.dismiss();
            }
            return alertText;
        } finally {
            acceptNextAlert = true;
        }
    }
}

我不知道哪里错了,指导我哪里错了

提前致谢

最佳答案

参见 org.openqa.jetty.html.List 的 javadoc:http://www.jarvana.com/jarvana/view/org/seleniumhq/selenium/selenium-server/2.0b1/selenium-server-2.0b1-javadoc.jar!/org/openqa/jetty/html/List.html

java.util.List 之一:http://docs.oracle.com/javase/7/docs/api/java/util/List.html

您使用的不支持泛型(如错误所述)。

您的问题似乎是以下导入:

import org.openqa.jetty.html.List;

尝试将其替换为:

import java.util.List;

有关更多想法,请参阅类似问题:The type Collection is not generic; it cannot be parameterized with arguments <? extends E>

关于java - Selenium 网络驱动程序 : <WebElement> Iterator cannot be resolved to a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17668329/

相关文章:

java - 无法加载编解码器 'Lucene410'

java - 错误:emulator-5554 disconnected:

java - 如何将Eclipse的自动插入分号移到光标之前?

perl - 使用 Perl 设置 Selenium WebDriver

Selenium GRID——调用 WebDriver.getCurrentUrl() 时, session 由于 TIMEOUT 在不到 100 毫秒后终止

javascript - 如何使用 Cypress.io 检查元素是否存在

java - 运行具有许多依赖项的 Maven 项目

java - System.getProperties 对于某些值返回 null

c++ - "g++"不被识别为内部或外部命令,MinGW

java - java企业应用中的log4j问题