[英]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: 这段代码有效但我需要修改脚本以便脚本:
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.