Hello,
This is asimple way to get a property of web elemnt if you knoen the locator of an element.
NOTE: remember if form name is different it will wont work. All code is tested.
Selenium RC Command are:
this color is element name
This color is property
1. *for checkBox and radio button*
browser.getEval("javascript:{var status = selenium.browserbot.getCurrentWindow().document.form.chkPTOPayment.checked; " +
"var val = selenium.browserbot.getCurrentWindow().document.form.*{color:#00ff00}chkPTOPayment*.*value*;" +
"var form = selenium.browserbot.getCurrentWindow().document.form.chkPTOPayment.form;"+
"var name = selenium.browserbot.getCurrentWindow().document.form.chkPTOPayment.name;"+
"var type = selenium.browserbot.getCurrentWindow().document.form.chkPTOPayment.type;"+
"var click = selenium.browserbot.getCurrentWindow().document.form.chkPTOPayment.click();"+
"var focus = selenium.browserbot.getCurrentWindow().document.form.chkPTOPayment.focus();"+
"var defaultChecked =Selenium.browserbot.getCurrentWindow().
document.form.chkPTOPayment.defaultChecked;"+
"alert(status+ \" \" + defaultChecked); }");
2. *for button element*
*note* : add alert box to seevalue of each property
browser.getEval("javascript:{var name = selenium.browserbot.getCurrentWindow().document.form.Image11.name;" +
"var value = selenium.browserbot.getCurrentWindow().document.form.Image11.value;"+
"var type = selenium.browserbot.getCurrentWindow().document.form.Image11.type;"+
"var disabled = selenium.browserbot.getCurrentWindow().document.form.Image11.disabled;"+
"var focus = selenium.browserbot.getCurrentWindow().document.form.Image11.focus();}");
3. *for input field property*
browser.getEval("javascript:{var value = selenium.browserbot.getCurrentWindow().document.form.name.value; " +
"var readOnly = selenium.browserbot.getCurrentWindow().document.form.name.readOnly;"+
"var name = selenium.browserbot.getCurrentWindow().document.form.name.name;"+
"var size = selenium.browserbot.getCurrentWindow().document.form.name.size;"+
"var defaultChecked = selenium.browserbot.getCurrentWindow().document.form.
chkPTOPayment.defaultChecked;"+
"var focus = selenium.browserbot.getCurrentWindow().document.form.chkPTOPayment.focus();"+
"var blur = selenium.browserbot.getCurrentWindow().document.form.chkPTOPayment.blur();"+
"alert(defaultChecked +\"\t \" + size); }");
4. *for select list property*
browser.getEval("javascript:{var length = selenium.browserbot.getCurrentWindow().document.form1.cboCurrency.length; " +
"var options = selenium.browserbot.getCurrentWindow().document.form1.cboCurrency.options;"+
"var selectedindex = selenium.browserbot.getCurrentWindow().document.form1.cboCurrency.selectedindex;"+
"var text = selenium.browserbot.getCurrentWindow().document.form1.cboCurrency.text;"+
"var focus = selenium.browserbot.getCurrentWindow().document.form1.cboCurrency.focus();"
"alert(text);"
"}" );
5. *for textArea property*
browser.getEval("javascript:{var name = selenium.browserbot.getCurrentWindow().document.form1.txtDesc_1.name;" +
"var readOnly = selenium.browserbot.getCurrentWindow().document.form1.txtDesc_1.readOnly;"+
"var size = selenium.browserbot.getCurrentWindow().document.form1.txtDesc_1.size;"+
"var type = selenium.browserbot.getCurrentWindow().document.form1.txtDesc_1.type;"+
"var value = selenium.browserbot.getCurrentWindow().document.form1.txtDesc_1.value;"+
"alert( type + \"\t\" + value);"+
"}");
6. *property for form method*
browser.getEval("javascript:{var action = selenium.browserbot.getCurrentWindow().document.form1.action;" +
"var elements =selenium.browserbot.getCurrentWindow().document.form1.elements;"+
"var length =selenium.browserbot.getCurrentWindow().document.form1.length;"+
"var method =selenium.browserbot.getCurrentWindow().document.form1.method;"+
"var submit =selenium.browserbot.getCurrentWindow().document.form1.submit();"+
"alert(elements);"
+"}");
Try to be more creative and used this way to get complex reult and tell me also how to go futher.
Best of luck.
GOD BLESS U ALL
By the way I tried runScript also and could not get even a single line executed!!!! It always returned null result.
Got it. Thanks to gim_h, I can do following -
String script = "var imgSrcArray = new Array();";
script += "var links = window.document.links;";
script = "for(var i=0; i<links.length; i+)";
script = "imgSrcArray = links+.href;";
script += "imgSrcArray.toString();";
String href = selenium.getEval(script);
System.out.println("Hyperlink Reference is: \n" + href);
Now to collect individual links in a string array I can use split method as following -
String[] hrefNumber = href.split(",");
// Print the individual elements.
for(int i=0; i<hrefNumber.length;i++){
System.out.println("Element at " i " position is: " hrefNumber );
}
Manoj,
Indeed it is a good document but I doubt if we can really execute multiple statement in one getEval function. As far as I know if there are multiple line then result of only last line would be evaluated Hence. Leave aside executing multiple statement if I use your syntax which involves javascript word in it then I can not get result for even a single statement. So what I have tried so far is -
////////////////////////////////////////////////////////////////////////////////////
selenium.getEval("javascript:{var name = selenium.browserbot.getCurrentWindow().document.getElementsByName('empNumber')[0].maxLength;" +
"var value = selenium.browserbot.getCurrentWindow().document.getElementsByName('empNumber')[0].size;}");
////////////////////////////////////////////////////////////////////////////////////
I expected that at least last statement would be executed and it would return me size of text box, but I got null result as following -
////////////////////////////////////////////////////////////////////////////////////
11:15:17.609 INFO - Command request: getEval[javascript:{var name = selenium.browserbot.getCurrentWindow().document.getElementsByName('empNumber')[0].maxLength;var value = selenium.browserbot.getCurrentWindow().document.getElementsByName('empNumber')[0].size;}, ] on session 6d900fc8ec174370947fbef91f0eb7c9
11:15:17.643 INFO - Got result: OK,null on session 6d900fc8ec174370947fbef91f0eb7c9
////////////////////////////////////////////////////////////////////////////////////
Instead of this is I use the following syntax then I get result for only last statement of expression -
////////////////////////////////////////////////////////////////////////////////////
selenium.getEval("window.document.getElementsByName('empNumber')[0].maxLength;" +
"window.document.getElementsByName('empNumber')[0].size");
////////////////////////////////////////////////////////////////////////////////////
Then I receive result as 20 which is the size of of text box hence only last statement gets executed. I know I can use multiple getEval statement to achieve this but what in the situation when I want to use loop in java script and do some operations in side the loop.
I feel since getEval just returns string and not string array hence it might not be able to store results of multiple lines of statement in java script. I might be wrong also.
Now can you please detail how you are able to get multiple lines executed in getEval function.
By the way I am using Selenium RC beta 1.0 with java client driver.
Thanks in advance.
Regards,
Tarun K