0
package Selenium.Locators;
import java.util.List;
import java.net.URL;
import java.util.ArrayList;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.firefox.FirefoxDriver;
import sun.net.www.protocol.http.HttpURLConnection;
public class program { 
// to get all the links in a website which has anchor tag and img tag
public static List findAllLinks(WebDriver driver)
{
    List elementList = new ArrayList();
    elementList = driver.findElements(By.tagName("a"));
    elementList.addAll(driver.findElements(By.tagName("img")));// to get the anchor tag and img tag values
    List finalList = new ArrayList(); 
    for (WebElement element : elementList)//it shows error in this line
    {
        if(element.getAttribute("href") != null)
        {
            finalList.add(element);
        }
    }
    return finalList;
}
// to find all the broken links in a  website
public static String isLinkBroken(URL url) throws Exception
{
    url = new URL("https://www.yahoo.com/");// to find the broken links 
    String response = ""
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    try
    {
        connection.connect();
        response = connection.getResponseMessage();
        connection.disconnect();
        return response;
    }
    catch(Exception exp)
    {
        return exp.getMessage();
    }
}
public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    System.setProperty("webdriver.gecko.driver", "G:\\AllLinks\\src\\test\\java\\Selenium\\Locators\\geckodriver.exe");
    FirefoxDriver ff = new FirefoxDriver();
    ff.get("https://www.yahoo.com/");
    List allImages = findAllLinks(ff);
    System.out.println("Total number of elements found " + allImages.size());
    for (WebElement element : allImages)// It shows the error in this line
        try
        {
            System.out.println("URL: " + element.getAttribute("href")+ " returned " + isLinkBroken(new URL(element.getAttribute("href"))));
            //System.out.println("URL: " + element.getAttribute("outerhtml")+ " returned " + isLinkBroken(new URL(element.getAttribute("href"))));
        }
        catch(Exception exp)
        {
            System.out.println("At " + element.getAttribute("innerHTML") + " Exception occured -> " + exp.getMessage());
        }
    }
}

If i run the code i get the following error message Error:(69, 35) java: incompatible types: java.lang.Object cannot be converted to org.openqa.selenium.WebElement This code is used for getting all the links in a website so that we can test it manually for finding all the element.

2
  • 2
    not a solution but you should use List<WebElement> Commented Oct 11, 2017 at 6:46
  • @ShekharSwami Beat me by a few seconds :D It might be a solution though because I think List is more or less List<Object>. By specifying WebElement the issue might be resolved. Unsure so I am writing a comment :) Commented Oct 11, 2017 at 6:47

3 Answers 3

0

As explained by @Shekhar Swami you should define list of web element as shown below

List<WebElement> elementList = driver.findElements(By.tagName("a"));
Sign up to request clarification or add additional context in comments.

Comments

0

In following line of your code:

List elementList = new ArrayList();

List is Generic interface in java you need to provide a type while initializing it. If you don't provide one, it will take java.lang.Object as its type by default.

for (WebElement element : elementList)

Here you are extracting each of the element on that list which have type Object, and your element variable is of type WebElement.

For making your code work. do the following changes in that line

List<WebElement> elementList = new ArrayList<WebElement>();

Reference for Generic Types in java : Click here

Comments

0

following is the error

Error:(69, 35) java: incompatible types: java.lang.Object cannot be converted to org.openqa.selenium.WebElement

it means, Your List is incompatible with WebElement , so you have to define and instantiate List as WebElement type like this

List<WebElement> elementList = driver.findElements(By.tagName("a"));

Try this and let me know

just for example I have used like this:

List<WebElement> TotalLinks = driver.findElements(By.tagName("a"));
System.out.println("Links count is: "+TotalLinks .size());
for(WebElement link : TotalLinks )
System.out.println(link.getText());

Comments

Your Answer

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.