Selenium webdriver:列表不是通用的;不能使用参数`< WebElement>`类型进行参数化
我试图将链接存储在列表中,请遵循以下代码
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
列表类型不是通用的;不能使用参数
<WebElement>
type
以下是您的问题的答案:
Here is the Answer to your Question:
该错误说明了所有The type List is not generic; it cannot be parameterized with arguments <WebElement> type
.这意味着,当您像在List<WebElement> linkElements
中配置List
时,意外地是从未定义它的java.awt.List
导入了它.因此是错误.
The error says it all 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:
作为一种解决方案,我使用了自己的代码导入了java.util.List
而不是java.awt.List
,并且您的代码块也可以正常工作:
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);
}
}
控制台上的输出为:
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.