简体   繁体   English

Java:Selenium:WebDriver:无法解析列表 <WebElement> 宾语; 获取错误“无法转换为java.lang.String”

[英]Java: Selenium: WebDriver: Unable to Parse a List<WebElement> Object; Get Error “cannot be cast to java.lang.String”

I am new to java programming and I have a question on parsing a List<WebElement> object. 我是java编程的新手,我有一个关于解析List<WebElement>对象的问题。 I have a selenium webdriver script (written in java) that parses through a List<WebElement> object. 我有一个selenium webdriver脚本(用java编写),它通过List<WebElement>对象进行解析。 Code Snippet of working code is below. 工作代码的代码片段如下。

// Create a List composed of objects from the Client_Totals table
List<WebElement> tdlist = driver.findElements(By.cssSelector("table[class='client_totals'] tr td"));        // 8-12-13 -- This Code Works -- KV

for(WebElement el: tdlist)
    {
    System.out.println(el.getText());
    }

This code works but I need to modify the script so that the script: 这段代码有效但我需要修改脚本以便脚本:

  1. checks for the following values: 检查以下值:
    • (a) 11 - 25 User Discount (a)11 - 25用户折扣
    • (b) $17.55 (c) $4829.40 (b)17.55美元(c)$ 4829.40
  2. Outputs messages that inform user if the values were successfully located or not. 输出消息,通知用户是否成功定位了值。

I began to write a new modified script (see below) using an iterator to loop through the tdlist but I am getting the following error: java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement cannot be cast to java.lang.String 我开始使用迭代器编写一个新的修改过的脚本(见下文)来遍历tdlist但是我收到以下错误: java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement cannot be cast to java.lang.String强制转换java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement cannot be cast to java.lang.String

// Create a List composed of objects from the Client_Totals table
List<WebElement> tdlist = driver.findElements(By.cssSelector("table[class='client_totals'] tr td"));
Iterator itr = tdlist.iterator();                                                                         
while(itr.hasNext())
    {
    String value= (String)itr.next();
    System.out.println("Value : "+value);
    }

My Assessment: I think the error occurs because the tdlist is of a different type and, to work, the tdlist needs to be "cast" as a string array. 我的评估:我认为发生错误是因为tdlist属于不同的类型,并且为了工作,tdlist需要被“强制转换”为字符串数组。 However, I am not sure how to do this. 但是,我不知道该怎么做。

Next, I also need to add nested IF ELSE statements in the while structure. 接下来,我还需要在while结构中添加嵌套的IF ELSE语句。 I've written pseudo code on what I think the appropriate code structure might look like: 我已经编写了伪代码,我认为适当的代码结构可能是这样的:

While there are elements in the array
    {
    Parse through the array
    If value "11 - 25 User Discount" is found
        Print message "11 - 25 User Discount was found"
    Else If value "$17.55" is found
        Print message "$17.55 was found"
    Else If value "$4829.40" is found
        Print message "$4829.40 was found"
    Else
End While Loop
}

Ok. 好。 Your problem is that tdList is a List<WebElement> , and WebElement cannot be cast to a String. 您的问题是tdList是List<WebElement> ,并且WebElement无法强制转换为String。

If you are using a compiler, you probably got a warning on the Iterator line, saying that it was generic. 如果您使用的是编译器,您可能会在Iterator行上收到警告,说它是通用的。 You need to have that line be Iterator<WebElement> iter = tdlist.iterator(); 您需要将该行设置为Iterator<WebElement> iter = tdlist.iterator(); . Then you can do itr.next().getText() and you don't have to cast anything! 然后你可以做itr.next().getText() ,你不必投任何东西!

Here is my guess: 这是我的猜测:

// Create a List composed of objects from the Client_Totals table
List<WebElement> tdlist = driver.findElements(By.cssSelector("selector text"));
for ( WebElement el: tdlist ) {
    System.out.println( el.getText() );
    Assert.assertTrue( el.getText().equalsIgnoreCase("a message"), 
        "There was no message."); 
    Assert.assertTrue( el.getText().equalsIgnoreCase("11 - 25 User Discount"), 
        "There was no 11 - 25 User Discount.");
    Assert.assertTrue( el.getText().equalsIgnoreCase("$17.55"), 
        "There was no $17.55.");
    Assert.assertTrue( el.getText().equalsIgnoreCase("$4829.40"), 
        "There was no $4829.40.");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Webdriver异常:无法将Double强制转换为java.lang.String - Webdriver Exception:Double cannot be cast to java.lang.String java.lang.ClassCastException:通过Selenium执行测试时,无法将java.base / java.lang.String强制转换为org.openqa.selenium.WebElement - java.lang.ClassCastException: java.base/java.lang.String cannot be cast to org.openqa.selenium.WebElement when executing test through Selenium 获取错误 java.lang.ClassCastException: java.lang.String 无法转换为 [Ljava.lang.Object; - getting error java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object; 为什么我只能在 Android 4 中得到 java.lang.Object[] cannot be cast to java.lang.String[][] 错误? - Why do I get a java.lang.Object[] cannot be cast to java.lang.String[][] error only in Android 4? 无法将java.lang.String强制转换为[Ljava.lang.Object; - java.lang.String cannot be cast to [Ljava.lang.Object; 如何解决此错误java.lang.Object []无法转换为java.lang.String []? - How would i fix this Error java.lang.Object[] cannot be cast to java.lang.String[]? 无法将java.lang.String强制转换为java.util.List - java.lang.String cannot be cast to java.util.List [java.lang.String中; 无法强制转换为java.lang.String - [java.lang.String; cannot be cast to java.lang.String JAVA“无法转换为java.lang.String” - JAVA “cannot be cast to java.lang.String” [Android]对象无法强制转换为java.lang.String Asynctask - [Android]Object cannot be cast to java.lang.String Asynctask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM