0

I am getting error"Type mismatch: cannot convert from element type Object to WebElement" when tried to run a java file to check broken images on webpage.

My Code :

 List elementList = new ArrayList(); 
 elementlist = driver.findElements(By.tagName("a")); 
 elementlist.addAll(driver.findElements(By.tagName("img"))); 
 List finalList = new ArrayList(); 
 for (WebElement element : elementlist)
 { 
 if(element.getAttribute("href") != null) 
 { 
 finalList.add(element); 
 } 
 } 

please help to solve it out. below is the complete code.

Helping Hands
2,5443 gold badges30 silver badges54 bronze badges
asked Jan 23, 2015 at 10:43
4
  • 1
    Please add more code, like the declaration of the list and which objects are put in it. Commented Jan 23, 2015 at 10:55
  • Hi Frederik Thanks for response please check below code List elementList = new ArrayList(); elementlist = driver.findElements(By.tagName("a")); elementlist.addAll(driver.findElements(By.tagName("img"))); List finalList = new ArrayList(); for (WebElement element : elementlist) { if(element.getAttribute("href") != null) { finalList.add(element); } } Commented Jan 23, 2015 at 11:15
  • 1
    Share your code by updating your question please. Commented Jan 23, 2015 at 11:15
  • User for loop like : Object element : elementList.toArray() Commented Jan 23, 2015 at 11:54

1 Answer 1

2

The problem is that you are not declaring your Lists correctly. You need to specify the type of object they should contain. In this case they need to be List<WebElement> and not just List.

 List<WebElement> elementList = new ArrayList(); 
 elementlist = driver.findElements(By.tagName("a")); 
 elementlist.addAll(driver.findElements(By.tagName("img"))); 
 List<WebElement> finalList = new ArrayList(); 
 for (WebElement element : elementlist)
 { 
 if(element.getAttribute("href") != null) 
 { 
 finalList.add(element); 
 } 
 } 
Yamikuronue
3,3924 gold badges22 silver badges45 bronze badges
answered Jan 23, 2015 at 17:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.