Cannot get a checkbox - geb

I cannot get the checkbox selector from the documentation working. Any idea why? $('checkbox').size() is always zero.
HTML page
<html>
<head>
<title>Geb</title>
</head>
<body>
<input type="checkbox" name="pet" value="dogs" checked="true" />
</body>
</html>
Groovy code
Browser.drive {
go "file:///home/zoran/page.html"
println $('checkbox').size() // is always zero
}

To select all checkboxes on a page using a jQuery selector you need to use:
$('input[type=checkbox]').size()

to select all checkboxes use jquery selector as
$('input:checkbox').size()

simply provide id to your checkbox
<input type="checkbox" id="chkPet" name="pet" value="dogs" checked="true" />
and then
$("#chkPet")

Related

How to get value from an HTA file using VBScript or VBA

I am running VBScript and trying to get values from checkbox in HTA file (HTA window is already displayed). What is important, VBScript is a separate file - this code shoud not be included in section in HTA file because of some reasons/restrictions.
I think I should set this HTA window and create object somehow, but really do not how.
<html>
<head>
<HTA:APPLICATION ID="objHTAHelpomatic" APPLICATIONNAME="Checklist" SCROLL="no" SINGLEINSTANCE="yes" WINDOWSTATE="maximize" />
</head>
<body>
<input type="checkbox" name="c1" value="1" checked>ONE<br>
<input type="checkbox" name="c2" value="1" checked>TWO<br>
<input type="checkbox" name="c3" value="1" checked>THREE<br>
</body>
</html>
Could anyone suggest me how to get those values? For example to double click on VBS file and display values in MsgBox?

PhantomJS or CasperJS form autosubmit

I am building a laboratory task for exploitation CSRF vulnerability.
I need a bot, who will visit my page and execute JS:
<html>
<head>
</head>
<body>
<form action="http://localhost:9010/csrf/register.php" method="POST" id="csrf-form">
<input type="hidden" name="login" value="casper" />
<input type="hidden" name="password" value="casper" />
<input type="submit" value="Submit request"/>
</form>
</body>
<script>document.getElementById("csrf-form").submit()</script>
</html>
I can't proceed it with PhantomJS or CasperJS.
I don't know how HTML code will look like for different students (form id attribute can be different), I just want to execute JS code on page.
CasperJS version 1.1.4 at /opt/casperjs, using phantomjs version 2.1.1
var casper = require('casper').create();
casper.start('http://127.0.0.1/mypage.html');
Since you do not know the form id attribute (and since other element attributes are specific to each student), you can use casper.fillXPath() to use general selectors to fill out and auto-submit your form.
Example:
casper.start('https://example.com/', function () {
this.fillXPath('form', {
'//form/input[#type="hidden"][1]': username,
'//form/input[#type="hidden"][2]': password,
}, true);
});
Otherwise, you can look into using casper.fill(), casper.fillSelectors(), or casper.fillLabels() if you know more information about the selectors being used in the form.
If you need to simply execute JavaScript into the Page DOM environment, you can use casper.evaluate().

How do i identify the 1st, 2nd and 3rd textboxes without using indexes(ex [1]) - xpath, selenium

Below is the code. can someone pls help me to identify 3 text boxes individually, without using indexes.
<html>
<head>
<title>test</title>
<script></script>
</head>
<body>
name:
<input type="text"/>
<br/>
name:
<input type="text"/>
<br/>
name:
<input type="text"/>
<div>testdiv</div>
</body>
</html>
You can try below expressions to match each input:
For the first one:
//input[not(preceding-sibling::input)]
For second:
//input[preceding-sibling::input and following-sibling::input]
For third:
//input[not(following-sibling::input)]

write Java Script timestamp to input field

I want to write timestamp generated by java script to a hidden form input field.The following code writes the timestamp to a paragraph(p element) but does not write to the input element.
It writes to one element but fails to do so to another element.
Can anybody throw light on this behaviour of the script?
<!DOCTYPE html>
<html>
<body>
<p id="demo">pkj</p>
<input id="ts" type="time" name="ts"></input>
<script>
var oD = new Date();
document.getElementById("ts").innerHTML = oD.getTime();//**FAILS**
document.getElementById("demo").innerHTML = oD.getTime();//**SUCCEEDS**
</script>
As per your advice I changed the input element's attribute from "innerHTML" to "**
"value" as shown in code below.It is working fine now.
The issue is SOLVED.
thanks
Jayk
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p id="demo" > </p>
<input id="ts" type="time" name="ts" value=""></input>
<script>
var oD = new Date();
document.getElementById("ts").value = oD.getTime();//**SUCCEEDS**
document.getElementById("demo").innerHTML = oD.getTime();//**SUCCEEDS**
var t=document.getElementById("demo").innerHTML
</script>
</body>
</html>
<input> by definition is an empty element, meaning there is no innerHTML, it has no permitted content. And there is no need for a closing tag (</input>).
https://developer.mozilla.org/en/docs/Web/HTML/Element/Input
You can change the property "value", a sort of default input by
document.getElementById("ts").value = oD.getTime();
Or you could add a label like
<label id="label">xyz</label><input id="ts" type="time" name="ts">
and change it with
document.getElementById("label").innerHTML = oD.getTime();

How to stay in Current Page

I have one form of user name and password fields followed by submit button. How do I stay in current page after clicking submit button without reloding page?
Any help is appreciated.
Try tis if you need a sumit button.
<input type="submit" onclick="return false" />
But you may want to use a simple clickable button if you don't wan't to sumbit your form on click... in this case, this should do the trick:
<input type="button" />
There are a couple of options. First of all in the markup for the submit button you can return false:
<input type="submit" onclick="return false;" />
The problem with the approach above is that it will cancel the submit, not sure if that's desired or not. Another approach you can take is to use something like jQuery to do an ajax request.
To do that you'd need to include jQuery:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
Then change your submit button to a regular button:
<input type="button" onclick="doAjaxCall();" />
The javascript function doAjaxCall would then be something like:
<script type="text/javascript">
function doAjaxCall() {
$.ajax({ url: "destinationurl.aspx", success: function(){
alert('success! insert success javascript code here');
}});
}
</script>