当前位置: 首页 > 知识库问答 >
问题:

不兼容的类型:java。lang.对象无法转换为组织。openqa。硒。网络元素

陆昂然
2023-03-14
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());
        }
    }
}

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

共有3个答案

仲绍晖
2023-03-14

以下是错误

错误:(69,35)java:不兼容类型:java。lang.对象无法转换为组织。openqa。硒。网络元素

这意味着,你的列表与WebElement不兼容,所以你必须像这样定义列表并实例化为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());
简学文
2023-03-14

在以下代码行中:

List elementList=new ArrayList()

List是java中的通用接口,初始化时需要提供一个类型。如果您不提供,它将默认java.lang.Object作为其类型。

for(WebElement:elementList)

这里,您正在提取列表中具有类型Object的每个元素,并且您的元素变量的类型为WebElement

让你的代码正常工作。在该行中执行以下更改

列表

java中泛型类型的参考:单击此处

曹和正
2023-03-14

正如@Shekhar Swami所解释的,您应该定义如下所示的web元素列表

List<WebElement> elementList = driver.findElements(By.tagName("a"));
 类似资料:
  • 我有一个Java方法,他用来比较数据: 但是对于这行

  • 我试图通过在一个单独的线程上进行工作并返回所需的对象来更改JavaFX中的GUI。然而,在完成工作和任务之后。setOnSucceeded()在尝试检索创建的对象时被触发,并出现错误“不兼容类型:对象无法转换为VideoScrollPane类型”。 我认为这与原始类型有关,因为这是在听众中发生的,但环顾四周后,我找不到我想要的建议。 任何可以散发的光芒都将不胜感激。

  • 问题内容: 我试图通过在单独的线程上进行工作并返回所需的对象来对JavaFX中的GUI进行更改。但是,在完成工作和task.setOnSucceeded()之后,我尝试检索创建的对象并得到错误“不兼容的类型:对象无法转换为VideoScrollPane类型”。 我认为这与原始类型有关,因为它发生在侦听器中,但是四处查看后,我找不到我想要的建议。 任何可以散发出的光将不胜感激。 问题答案: 这是因为

  • 我尝试使用selenium测试登录功能,但我得到了这个例外: 这是我的代码: 注意:我尝试使用和xpath,但仍然不起作用。我也尝试在本文中使用一些解决方案,但没有结果 这是Html页面使用框架的菜单和其他表单登录页面 SCT公司 :

  • 问题内容: 我试图在我的项目中使用对象数组,但出现错误: 在这条线上: 现在我不知道此错误的原因是什么。请帮我 。 问题答案: 您有两种方法,一种是将每个对象emt1,emt2,..强制转换为String,如下所示: 或者,您应该更改属性的类型: 到弦

  • 我收到一个错误,指出ArrayList不能在我的上转换为int 我正在编写一个程序,在这个程序中,我必须从一个文件中读取所有整数,并计算它们的总和。我还处理所有异常,因此如果抛出异常,函数应该返回0。 这是我的代码: 下面是我的单元测试,读取文件后应该通过: 感谢您的帮助。 谢谢!