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());
}
}
}
If i run the code i get the following error message Error:(69, 35) java: incompatible types: java.lang.Object cannot be converted to org.openqa.selenium.WebElement This code is used for getting all the links in a website so that we can test it manually for finding all the element.
如@Shekhar Swami所述,您应该定义网络元素列表,如下所示
List<WebElement> elementList = driver.findElements(By.tagName("a"));
In following line of your code:
List elementList = new ArrayList();
List is Generic interface in java you need to provide a type while initializing it. If you don't provide one, it will take java.lang.Object as its type by default.
for (WebElement element : elementList)
Here you are extracting each of the element on that list which have type Object, and your element
variable is of type WebElement
.
For making your code work. do the following changes in that line
List<WebElement> elementList = new ArrayList<WebElement>();
Reference for Generic Types in java : Click here
following is the error
Error:(69, 35) java: incompatible types: java.lang.Object cannot be converted to org.openqa.selenium.WebElement
it means, Your List is incompatible with WebElement
, so you have to define and instantiate List as WebElement
type like this
List<WebElement> elementList = driver.findElements(By.tagName("a"));
Try this and let me know
just for example I have used like this:
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());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.