[英]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());
}
}
}
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. 如果我运行该代码,则会收到以下错误消息错误:(69,35)java:不兼容的类型:java.lang.Object无法转换为org.openqa.selenium.WebElement此代码用于获取所有链接。网站,以便我们可以手动对其进行测试以查找所有元素。
如@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. List是Java中的Generic接口,您需要在初始化它时提供一个类型。 If you don't provide one, it will take java.lang.Object as its type by default.
如果不提供,默认情况下它将使用java.lang.Object作为其类型。
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
. 在这里,您将提取该列表上具有Object类型的每个元素,并且您的
element
变量的类型为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 Java中泛型类型的参考: 单击此处
following is the error 以下是错误
Error:(69, 35) java: incompatible types: java.lang.Object cannot be converted to org.openqa.selenium.WebElement
错误:(69、35)Java:不兼容的类型:java.lang.Object无法转换为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 这意味着,您的列表与
WebElement
不兼容,因此您必须像这样将List定义和实例化为WebElement
类型
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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.