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"));
ないソリューションがありますが、数秒で私にビートリスト –
@ShekharSwamiを使用する必要があります:Dそれを私はListが多かれ少なかれリストだと思うので解決策かもしれない。