unable to typecase from webElement to String

491 views
Skip to first unread message

Prabu Elamurugu selvan

unread,
Nov 5, 2015, 4:49:27 PM11/5/15
to webdriver

Hi,

When i am type casting from webelement type to string Type i am getting the below error.

Error msg :
Exception in thread "main" java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement cannot be cast to java.lang.String
at JavaSelenium.TestSelenium.testObj.main(testObj.java:45)


package JavaSelenium.TestSelenium;

import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.support.ui.Select;

public class testObj {

public static void main(String args[]) throws InterruptedException {
// ProfilesIni Prof = new ProfilesIni();
// FirefoxProfile pro = Prof.getProfile("Selenium");
// WebDriver driver = new FirefoxDriver(pro);
WebDriver driver = new FirefoxDriver();
Thread.sleep(6000);
driver.manage().window().maximize();
WebElement element = driver.findElement(By.id("DDSrchPropertyType"));
Select select= new Select(element);
List<WebElement> options = select.getOptions();
Iterator it= options.iterator();
String Str;
while (it.hasNext()) {
 Str =  (String) it.next(); // unable to typecast 
System.out.println(it.next());
}
driver.close();
}
}

Please let me know how to resolve this issue.

Thanks
Prabu

Krishnan Mahadevan

unread,
Nov 6, 2015, 5:26:57 AM11/6/15
to webdriver
The problematic lines are 
 Str =  (String) it.next(); // unable to typecast 
System.out.println(it.next());


You would need to get rid of the type casting that you are doing and instead just resort to using : System.out.println(it.next()); and see if that helps

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages