简体   繁体   中英

Selenium webdriver : List is not generic; it cannot be parameterized with arguments `<WebElement>` type

I was trying to store link in List, follow below code

public class frameswitch {

public static void main(String[] args) {

    System.setProperty("webdriver.gecko.driver","C:\\geckodriver\\geckodriver.exe");  
    WebDriver driver=new FirefoxDriver();
    String baseUrl="https://www.udacity.com/";
    driver.get(baseUrl);
    String Title="Udacity - Free Online Courses and Nanodegree Programs";
    List<WebElement> linkElements = driver.findElements(By.tagName("a"));
    }
}

but facing error while using list

The type List is not generic; it cannot be parameterized with arguments <WebElement> type

Here is the Answer to your Question:

The error says it all The type List is not generic; it cannot be parameterized with arguments <WebElement> type The type List is not generic; it cannot be parameterized with arguments <WebElement> type . It means when you configured the List as in List<WebElement> linkElements , accidentally you have imported it from java.awt.List where it is not defined. Hence the error.

The following screenshot shows it all:

在此处输入图片说明

Solution:

As a solution, I have used your own code importing java.util.List instead of java.awt.List and your code block works just fine:

package demo;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Q45402867_tagname_a {

    public static void main(String[] args) {
        System.setProperty("webdriver.gecko.driver","C:\\Utility\\BrowserDrivers\\geckodriver.exe");  
        WebDriver driver=new FirefoxDriver();
        String baseUrl="https://www.udacity.com/";
        driver.get(baseUrl);
        String Title="Udacity - Free Online Courses and Nanodegree Programs";
        List<WebElement> linkElements = driver.findElements(By.tagName("a"));
        System.out.println(linkElements.size());
        for (WebElement ele:linkElements)
        System.out.println(ele);
    }

}

The output on the console is:

86
[[FirefoxDriver: firefox on ANY (ef81931f-9530-4998-8405-6581ab51c86e)] -> tag name: a]
...  84 more ...
[[FirefoxDriver: firefox on ANY (ef81931f-9530-4998-8405-6581ab51c86e)] -> tag name: a]

Let me know if this Answers your Question.

Check your import you have imported List from

java.awt.List

instead of

java.util.List

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