簡體   English   中英

不兼容的類型:java.lang.Object無法轉換為org.openqa.selenium.WebElement

[英]incompatible types: java.lang.Object cannot be converted to org.openqa.selenium.WebElement

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此代碼用於獲取所有鏈接。網站,以便我們可以手動對其進行測試以查找所有元素。

如@Shekhar Swami所述,您應該定義網絡元素列表,如下所示

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

在您的代碼的以下行中:

List elementList = new ArrayList();

List是Java中的Generic接口,您需要在初始化它時提供一個類型。 如果不提供,默認情況下它將使用java.lang.Object作為其類型。

for (WebElement element : elementList)

在這里,您將提取該列表上具有Object類型的每個元素,並且您的element變量的類型為WebElement

用於使您的代碼正常工作。 在該行中進行以下更改

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

Java中泛型類型的參考: 單擊此處

以下是錯誤

錯誤:(69、35)Java:不兼容的類型:java.lang.Object無法轉換為org.openqa.selenium.WebElement

這意味着,您的列表與WebElement不兼容,因此您必須像這樣將List定義和實例化為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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM