How to Serialize the webelement?

857 views
Skip to first unread message

Raihan

unread,
May 13, 2011, 6:36:09 PM5/13/11
to Selenium Users
Hi

I wan to save all the detected elements of a web page in a file . i
was trying the following example but getting following error.

Caused by: java.io.NotSerializableException:
org.openqa.selenium.firefox.FirefoxWebElement
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)

any one any idea how to solve it..



TheCode.
=======================
public class Selenium1Example {



public static void main(String[] args) {
System.out.println("Initiation Starts");
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://www.ltu.se";
driver.get("http://www.ltu.se");
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
selenium.open("http://www.ltu.se");
selenium.waitForPageToLoad("90000");
// Get the underlying WebDriver implementation back. This will refer
to the
// same WebDriver instance as the "driver" variable above.

String s ="C:\\object.txt";
FileOutputStream fos = null;
ObjectOutputStream outObject = null;
try
{
fos = new FileOutputStream(s);
outObject = new ObjectOutputStream(fos);
//out.writeObject(time);
//out.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
elementSerialized es1;
List<WebElement> allOptions = driver.findElements(By.tagName("a"));
for (WebElement option : allOptions)
{

es1 =new elementSerialized(option);

try
{
outObject.writeObject(es1);
}
catch(Exception e)
{
e.printStackTrace();
}
break;
}
try
{
outObject.close();
}
catch(Exception e)
{
e.printStackTrace();
}
//Finally, close the browser. Call stop on the
WebDriverBackedSelenium instance
//instead of calling driver.quit(). Otherwise, the JVM will
continue running after
//the browser has been closed.
//selenium.stop();

WebElement e2=null;
elementSerialized es2=null;
FileInputStream fis = null;
ObjectInputStream in = null;
try
{
fis = new FileInputStream(s);
in = new ObjectInputStream(fis);
es2 = (elementSerialized)in.readObject();
e2=es2.getWebElement();
in.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}




}
}


public class elementSerialized implements Serializable
{
private WebElement element;
public elementSerialized(WebElement e1)
{
element=e1;
}
public void setElement(WebElement e1)
{
element=e1;
}

public WebElement getWebElement()
{
return element;
}
}

Lutfi Dughman

unread,
May 13, 2011, 7:14:52 PM5/13/11
to seleniu...@googlegroups.com
in order for an object to be serilizable, it has to extend Serializable interface, also every object that is part of the webelement has to be serializable too (only exception is primitives ints/longs/arrays), 

I took a fast look at the type hierarchy for WebElement and it doesnt implement Serializable.


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


Jeff

unread,
May 13, 2011, 9:36:39 PM5/13/11
to seleniu...@googlegroups.com
Even if it did implement serializable, the object is really a reference (via JNI ?) into the DOM for the current browser in the current page.  As soon as any of those elements changes it invalidates the WebElement and you'd get something like a StaleElementException as soon as you tried to use it, so if you are trying to save the state (attributes and values), this won't work.
Depending on your need, you might be better off creating your own wrapper class that caches the desired element state (assuming you want to manipulate the values after the browser/page is long gone) and/or serializing the locator so you can call findElement() again at a later time.
 
What is the use case you are trying to accomplish?
--
I ♥ DropBox !! 

Raihan -Ul - Islam

unread,
May 13, 2011, 10:08:22 PM5/13/11
to seleniu...@googlegroups.com
Thanks for the suggestion. i think i am not using the right tool. Actually i want to control the browser and web pages using a motion controller device.. can u advice me any other api or framework. Sorry for putting wrong question in the group

Thanks
Raihan

Lutfi Dughman

unread,
May 13, 2011, 10:36:21 PM5/13/11
to seleniu...@googlegroups.com
motion control? kinect? i gues using kinect you can control the mouse or something. there is a kinect SDK for windows so u can go ahead and give it a try.

or you can use one of the cellphones with gyroscopes and orientation sensors, and get data from them.

Raihan -Ul - Islam

unread,
May 13, 2011, 10:52:45 PM5/13/11
to seleniu...@googlegroups.com
I am doing it using kinect. actually i am stuck at two things 
1) Is to scroll the page. How can i send the browser the instruction of scrolling.
2) To know the browser location in the screen.

Lutfi Dughman

unread,
May 13, 2011, 11:44:53 PM5/13/11
to seleniu...@googlegroups.com
ok, I assume then that you are using the SDK.

long time ago i had a very simple visual basic applicatio, that used to do the following

1- get a handle to a window based on its name or title
2- send the app, windows Os messages.

now these messages can be anything from left click/right click/mouse wheel scroll up/ mouse wheel scrol down.


what i suggest that you go through the SDK and try to mimick this with IE or whatever browesre you are using



this shoudl give you some info about windows messaging through visual basic

http://support.microsoft.com/kb/168204

Raihan -Ul - Islam

unread,
May 14, 2011, 5:21:30 AM5/14/11
to seleniu...@googlegroups.com
But how to scroll the browser.. 
Reply all
Reply to author
Forward
0 new messages