【问题标题】:Type safety: Unchecked cast from WebElement to List<WebElement>类型安全:从 WebElement 到 List<WebElement> 的未经检查的强制转换
【发布时间】:2021-10-11 11:16:30
【问题描述】:

我的代码:- 文件1.java

public int isListAvailable(String locator) throws Exception {
        WebElement ele = getObject(locator);
        List<WebElement> ls = **(List<WebElement>) ele;**
        //List<WebElement> ls1 = driver.findElements((By) ele);
        //List<WebElement> ls2 = driver.findElements(ele);
        int rowCount = ls.size();
        System.out.println("Last1 row=" + rowCount);        
        return rowCount;
    }

    public WebElement getObject(String locatorKey) throws Exception {
            WebElement ele = null;
            WebDriverWait wait = new WebDriverWait(driver, 10);
            try {
                if (locatorKey.endsWith("_xpath")) {
                    ele = driver.findElement(By.xpath(prop.getProperty(locatorKey)));        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(prop.getProperty(locatorKey))));
    }

.....
...
....

    }catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
    }
            return ele;
    }

File2.java(catlisting_xpath 是元素的 XPATH)

    public void search_List() throws Exception {
            
            if(con.isListAvailable("catlisting_xpath") >=1)
            {
                con.infoLog("Category List is available");
            }else {
                con.infoLog("Category List is not available");
            }
        }

enter image description here

错误:-

java.lang.ClassCastException:类 org.openqa.selenium.remote.RemoteWebElement 无法转换为类 java.util.List(org.openqa.selenium.remote.RemoteWebElement 在加载程序“app”的未命名模块中;java .util.List 位于加载器“bootstrap”的模块 java.base 中)

enter image description here

问题是,当我在 File1.java 上方运行或键入时,在 List ls = (List) ele; 处收到警告。 警告是 类型安全:从 WebElement 到 List 的未经检查的强制转换

谁能帮帮忙,如何解决这个问题...

【问题讨论】:

    标签: java selenium runtime-error typecasting-operator


    【解决方案1】:
    WebElement ele = getObject(locator);
    List<WebElement> ls = (List<WebElement>) ele;
    

    您将List 转换为WebElement,这是无效的。我相信您使用findElement 方法来识别定位器的getObject 方法。而不是使用findElements 方法来获取ListWebElement

    尝试如下,

    List<WebElement> ls = driver.findElements(By.xpath(locator));
    

    【讨论】:

    • 谢谢,完全同意你的看法......
    【解决方案2】:

    这非常复杂。

    演员表运算符 ((SomeType) someExpr)) 做了 3 件几乎完全不相关的事情。我想你在这里混淆了他们中的一些人。

    如果SomeType 是原始数字类型(intchardoublefloatlongbyteshort),则 强制转换一件事到另一件事

    但这就是演员表转换方面的结束。在所有其他情况下,您只是告诉编译器:我知道该表达式属于我告诉您的类型。有时,会添加运行时检查(如果表达式结果不是该类型,则会抛出 ClassCastException)。有时,编译器只是相信你的话。

    泛型部分 (&lt;WebElement&gt;) - 该部分是编译器将接受您的话的地方,并且会生成警告

    但在这种情况下,该警告完全无关紧要:问题根本不在于那部分。获得运行时检查的非泛型部分(List)。因为它会进行运行时检查,所以编译器不会发出警告:没关系 - 如果你的类型断言被证明是错误的,在运行时你会得到一个异常通知你。

    因此,警告与 &lt;WebElement&gt; 部分有关,但您在运行时收到的错误与 List 部分有关。

    您的 WebElement 不是列表。也许您正试图创建一个包含单个元素的列表。在这种情况下,您需要List.of(ele);。正如我所说,类型断言模式转换不会转换任何内容,因此您不能使用它将元素转换为仅包含该元素的列表。

    【讨论】:

      猜你喜欢
      • 2022-02-03
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 2013-12-15
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多