不兼容的类型:java.lang.Object无法转换为org.openqa.selenium.WebElement

时间:2017-10-11 06:43:15

标签: java selenium import

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());
        }
    }
}

如果我运行代码我得到以下错误消息错误:(69,35)java:不兼容类型:java.lang.Object无法转换为org.openqa.selenium.WebElement 此代码用于获取网站中的所有链接,以便我们可以手动测试它以查找所有元素。

3 个答案:

答案 0 :(得分:0)

正如@Shekhar Swami解释的那样,你应该定义web元素列表,如下所示

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

答案 1 :(得分:0)

在您的代码的以下行中:

List elementList = new ArrayList();

List是java中的通用接口,您需要在初始化时提供类型。如果你没有提供一个,默认情况下它将以java.lang.Object为其类型。

for (WebElement element : elementList)

在这里,您要提取该列表中具有Object类型的每个元素,并且您的element变量的类型为WebElement

使您的代码有效。在该行中进行以下更改

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

java中的泛型类型参考:Click here

答案 2 :(得分:0)

以下是错误

  

错误:(69,35)java:不兼容的类型:java.lang.Object不能   转换为org.openqa.selenium.WebElement

这意味着,您的列表与WebElement不兼容,因此您必须像这样定义和实例化列表WebElement类型

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

试试这个并告诉我

就像我曾经这样使用过:

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());