Problem with using List<WebElement>

2,949 views
Skip to first unread message

Harish Goud

unread,
Nov 28, 2013, 9:46:30 AM11/28/13
to seleniu...@googlegroups.com
Hi,

I am using below code to get the all links in to string using webdriver.

List<WebElement> linkElements=driver.findElement(By.tagName("a"));
int linkl=linkElements.size();
for(WebElement myelement: linkElements)
{
String link= myelement.getText();
System.out.println(link);
}
I am new to webdriver, not able to run the code and getting below error, can someone help me on this please?

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The type List is not generic; it cannot be parameterized with arguments <WebElement>


Thanks in advance,
Harish

shadab 03

unread,
Nov 29, 2013, 12:15:03 AM11/29/13
to seleniu...@googlegroups.com
Harish,

Try this way hope it ll help you

List<WebElement> allLinks = driver.findElements(By.tagName("a"));
System.out.println(allLinks.size());
for (int i=0;i<allLinks.size();i++){
System.out.println(allLinks.get(i).getText());
}


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/779cfd0a-341e-4971-acba-1cf220a1d7a3%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Dan Flucus

unread,
Nov 29, 2013, 9:01:05 AM11/29/13
to seleniu...@googlegroups.com
Hi,

The first line should be:


List<WebElement> linkElements=driver.findElement(By.tagName("a"));

and make sure you import: import java.util.List;

Shawn McCarthy

unread,
Nov 29, 2013, 12:21:09 PM11/29/13
to seleniu...@googlegroups.com
You are using findElement when you should be using findElements (with an s). The findElement returns a single element. The findElements returns a list of WebElement's.

DongXiang

unread,
Dec 3, 2013, 2:31:53 AM12/3/13
to seleniu...@googlegroups.com
HI

it should be findElement(By by); findElement() only return WebElement. findElements() return list.

if you use eclipse, move your mouse to the red flag, you will see the cause.

Best Regards,
--david 


Date: Fri, 29 Nov 2013 06:01:05 -0800
From: danf...@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Problem with using List<WebElement>

Harish Goud

unread,
Dec 3, 2013, 8:30:45 AM12/3/13
to seleniu...@googlegroups.com
Hi,

Many thanks all who replied to this post.
it worked for me, i have used findElements()  instead of findElement().

Thanks again:)

Regards,
Harish

Deepak Dash

unread,
Apr 16, 2017, 8:05:33 AM4/16/17
to Selenium Users
Hello,

Instead of List<WebElement>, I have used below and it is working fine, hope it should work for u as well.

java.util.List<WebElement>e=drop.getOptions();
int count=e.size();
System.out.println(count);

Thanks
Deepak
Reply all
Reply to author
Forward
0 new messages