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

に変換することはできません。 lang.Objectはorg.openqa.selenium.WebElementに変換できません このコードは、すべての要素を見つけるために手動でテストできるように、Webサイト内のすべてのリンクを取得するために使用されます。 @Shekharスワミで説明したように、次のコードの行では互換性のない型:(69、35)のjava:互換性のない型:javaのjava.lang.Objectのは、私は、コードを実行する場合、私は次のエラーメッセージエラーを取得org.openqa.selenium.WebElement

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

ないソリューションがありますが、数秒で私にビートリスト

+0

@ShekharSwamiを使用する必要があります:Dそれを私はListが多かれ少なかれリストだと思うので解決策かもしれない。。 WebElementを指定すると、問題が解決される可能性があります。私はコメントを書いているので、わかりません:) – geisterfurz007

答えて

0

するとは、ウェブ要素のリストを定義する必要があります。

List elementList = new ArrayList();

リストジェネリックインターフェイスはJavaであります型を初期化しながら型を提供する必要があります。提供しない場合、デフォルトでjava.lang.Objectが型として使用されます。ここで

for (WebElement element : elementList)

あなたはObject型を持つリストの要素のそれぞれを抽出している、とあなたelement変数の型WebElementです。

コードを作成するため。 Javaで汎用型のため、その行に以下の変更を行います

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

参考:以下Click here

0

はあなたのリストがそう、WebElementと互換性がありません、エラー

Error:(69, 35) java: incompatible types: java.lang.Object cannot be converted to org.openqa.selenium.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()); 
関連する問題