2017-10-11 339 views
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()); 
     } 
    } 
} 

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

+2

不是一个解决办法,但你应该使用列表

+0

@ShekharSwami几秒打我:d它可能是一个解决方案,但因为我认为列表或多或少列表。通过指定WebElement,问题可能会得到解决。不确定所以我写评论:) – geisterfurz007

回答

0

正如@Shekhar斯瓦米解释你应该定义如下图所示

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

在下面一行代码的网页元素的列表:

List elementList = new ArrayList();

列表是通用的接口在Java中你需要在初始化时提供一个类型。如果你不提供它,默认情况下它将使用java.lang.Object作为它的类型。

for (WebElement element : elementList)

这里你正在提取每一个列表,它具有Object类型的元素,你的element变量是WebElement类型。

为了使您的代码工作。做以下修改该行

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

参考在Java泛型类型:Click here

0

以下是错误

Error:(69, 35) java: incompatible types: java.lang.Object cannot be converted to 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()); 
相关问题