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.

  • 1
    Please add more code, like the declaration of the list and which objects are put in it. – FDM Jan 23 '15 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); } } – Rajesh Rathod Jan 23 '15 at 11:15
  • 1
    Share your code by updating your question please. – Helping Hands Jan 23 '15 at 11:15
  • User for loop like : Object element : elementList.toArray() – Helping Hands Jan 23 '15 at 11:54
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); 
           }    
       } 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.