casperjs login form error - javascript

I am using this code to login to form. I get error.
var casper = require('casper').create();
var url = "www.example.com";
casper.start(url,function(){
this.echo(this.getTitle());
});
casper.waitForResource(url,function() {
this.fillSelectors("form[method='post']", {
'input[name="userName"]' : 'usr1',
'input[name="password"]': 'pswed1'
}, true);
});
casper.run();
http://prntscr.com/7jkia1
I pretty much copied examples I found on stack but still I get error... what to do to fix this?

It seems that casper is not able to do a selector for form[method='post'] ( using fillSelectors ), its not working for me either. But if you specify any other atribute like name, id, class, enctype, action, etc, it works.
var casper = require('casper').create();
var url = "https://careershub.bbc.co.uk/members/";
casper.start(url,function(){
this.echo(this.getTitle());
});
casper.waitForResource(url,function() {
this.fillSelectors('form#registration-form', {
'input[name="name_first"]' : 'firstname',
'input[name="name_last"]': 'lastname'
}, true);
this.captureSelector('test.png', 'form');
});
casper.run();

Related

Bypass log .js file

i want to bypass this login because i don't have the rest of the source /core
*sorry in advanced because i am a newbie i only know python and php :3
var lgnusr = $('#lgnusr'),lgnpss = $('#lgnpss');
$(document).ready(function(){
//---------------------------------------
$('.inpx input').on('focus',function(){
$(this).parent().addClass('animat');
}).on('blur',function(){
$(this).parent().removeClass('animat');
if($('.denger').hasClass('hide') === false){$('.denger').addClass('hide');}
}) ;
$('#signbtn').on('click',function(){
if(isempty(lgnusr)){lgnusr.parent().addClass('error');}else if(isempty(lgnpss)){lgnpss.parent().addClass('error');}else{
var obj = {user:lgnusr.val().trim(),pass:lgnpss.val().trim(),csrf:$(this).data('csrf'),rem:$('#remmber').prop('checked')};
$('.loading').removeClass('hide');
$.post("./core/",{qlogin:JSON.stringify(obj)},
function (data) {
$('.loading').addClass('hide');
if(data.ok === true){window.location.href = './dashboard'}else{$('.denger').removeClass('hide');}
},
"json"
).fail(function(){alert('Invalid Request!! Please Refresh The Page and Try Again..');});
}
});
//--------------------------------------
});```
you replace the fail action:
fail(function(){alert('Invalid Request!! Please Refresh The Page and Try Again..');});
by
fail(function(){ window.location.href = './dashboard';});
you type any password and any user

CasperJS; opening dynamic url

I am using casperJS to got ta a page and collect an id from the URL, this is what my start function is doing. I would then like to put the id in to a new url and go there.
This works for the user var, this is coming in from the terminal. However sid is undefined and not the value I updated it to.
Any help would be appreciated. I am wondering if I found a limitation with casperJS. please not that this is not the full code (as that is much longer), if you message me I can provide you the whole script.
var user = decodeURIComponent(casper.cli.get(0).replace(/\+/g, ' '))
var sid
casper.start('https://editor.storify.com/', function() {
sid = this.getCurrentUrl()
sid = sid.split('/')[3]
})
casper.thenOpen('https://storify.com/' + sid, function() {
console.log('lets view the posts')
console.log(token)
console.log(sid)
if (dev)
this.capture('storeheading.png')
})
you can solve this problem by warping the thenOpen inside of a then
casper.then(function() {
casper.thenOpen('https://storify.com/' + sid, function() {
console.log('lets view the posts')
console.log(token)
console.log(sid)
if (dev)
this.capture('storeheading.png')
})
})

AngularJS - Facebook Sharer

I am trying to use the Facebook Share in AngularJS. Below is my function that is called when the user clicks on the FB icon.
$scope.shareFB = function(){
// Get configuration ID from service
configuratorService.storeConfiguration($scope.modelCode, function(configID){
// Use saved configuration id to create share link
var base = $location.absUrl().replace($location.url(), '');
var byoUrl = base + "/" + $scope.modelCode + "/resume/" + configID;
console.log(byoUrl);
var fbpopup = window.open("https://www.facebook.com/sharer/sharer.php?u=" + byoUrl, "pop", "width=600, height=400, scrollbars=no");
});
}
This function works fine when I try to share a url like "https://www.google.com/"
the Facebook Popup then has the URL = "https://www.facebook.com/sharer/sharer.php?u=https://www.google.com/"
When I use the function above:
byoUrl = "http://localhost:8000/#/15K6/resume/9295316837"
and the resulting FB popup has URL = "https://www.facebook.com/15K6/resume/9295316837"
Why does the "/sharer/sharer.php?=http://localhost:8000/#/" get cut off?
You shouldn't even try to share a localhost URL, as Facebook will never be able to scrape it. That's very likely why your URL gets cut off. Facebook tries to resolve it and scrape it, but it will never find it, so it makes a best effort to redirect within itself. Example:
https://www.facebook.com/sharer/sharer.php?u=http://localhost:8000/#/coke
Try to put your share logic in the controller. Something along these lines.
// Share posts
$scope.fbShare = function(post){
FB.ui(
{
method: 'feed',
name: post.title,
link: 'http://www.cengkuru.com/'+post.slug,
picture: '',
caption: '',
description: $filter('limitTo')($scope.post.body, 150),
message: ''
});
}
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: 'YOUR_APP_ID', status: true, cookie: true,
xfbml: true});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>

Proplem with url using in Casperjs

I have problem with the url of my website that I'm doing on it with casperjs.I want to get title and test the class or id that have in this page,and set the value to textbox in this website by using casperjs.
//var url = 'https://www.google.com.kh/';
//this url below it is not work with my code if the url above it work normal
var url = 'https://teleservices.paris.fr/etatcivil/jsp/site/RunStandaloneApp.jsp?page=formengine&form=naissance';
var casper = require('casper').create();
casper.start(url, function() {
this.echo(url);
this.echo("start page...");
});
casper.then(function(){
this.wait(1000,function(){
this.echo('Page: ' + this.getTitle());
});
}),
casper.then(function() {
if(this.exists("div.job-search-box-text")){
this.echo("this is id found");
}
else{
this.echo("no ID found");
}
}),
casper.then(function(){
this.wait(1000,function(){
this.fillSelectors('form', {
'input[name="title"]' : "12"
});
this.capture('result.png');
});
});
casper.run();
I have the problem with this url when I set the var url = 'https://teleservices.paris.fr/etatcivil/jsp/site/RunStandaloneApp.jsp?page=formengine&form=naissance'; my result code can not display the title of the web page,can not get the each id or class in this webpage,can not capture....I'm stuck with this anyone have solution , I have tried it for long time,please help me, thanks.
Wow - I can't figure out that is going on here. I got the code to work but I wasn't able to figure out exactly what fixed it.
There are a few oddities with the site, sometimes an image will 404 for me and I noticed an odd url structure. Note the //css/
https://teleservices.paris.fr/etatcivil//css/plugins/images/local/skin/plugins/etatcivil/left-first-srub.gif
I wish there was some key lesson to give you here - I am sure there is but I don't know it :)
var url = "https://teleservices.paris.fr/etatcivil/jsp/site/RunStandaloneApp.jsp?page=formengine&form=naissance";
var casper = require('casper').create({
logLevel:"debug",
verbose: true,
waitTimeout: 6000
});
casper.userAgent('Mozilla/4.0 (compatible; MSIE 10.0; Windows NT 6.1)');
casper.start(url, function() {
this.echo(url);
this.echo("start page...");
});
casper.waitForSelector('#formengine-menu', function() {
this.echo('Page: ' + this.getTitle());
});
casper.then(function() {
if(this.exists("#naissancelieuEtDate")){ this.echo("Form naissancelieuEtDate exists"); }
else{ this.echo("no ID found"); }
});
casper.then(function(){
this.fill('#naissancelieuEtDate', {
'dateActeJour' : "12"
});
this.capture('result.png');
});
casper.run();
I'm using casperjs 1.0.2 on OSX mavericks and I don't have problem to display the title.
You check if div.job-search-box-text exists but it does not exists in the two cases.
And the fillSelectors code which output error : there is no input field with a name title.
here is the output for the url https://www.google.com.kh/
https://www.google.com.kh/
start page...
Page: Google
no ID found
TypeError: 'undefined' is not a function (evaluating 'this.fillSelectors('form', {'input[name="title"]' : "12"})')
/Users/jeff/Downloads/code/automation/casperjs-1.0.3/scripts/test.js:24
/Users/jeff/Downloads/code/automation/casperjs-1.0.3:1677 in _check
here is the output for the url https://www.google.com.kh/
https://teleservices.paris.fr/etatcivil/jsp/site/RunStandaloneApp.jsp?page=formengine&form=naissance
start page...
Page: Acte de naissance
no ID found
TypeError: 'undefined' is not a function (evaluating 'this.fillSelectors('form', {'input[name="title"]' : "12"})')
/Users/jeff/Downloads/code/automation/casperjs-1.0.3/scripts/test.js:24
/Users/jeff/Downloads/code/automation/casperjs-1.0.3:1677 in _check

How to retrieve attribute of element in CasperJS using an XPath expression

I have a webpage with this between lines:
<a href="http://foo.com/home.do?SID=3443132">...
I need to extract "href" attribute using XPath. In the API of CasperJS is wrote this information about this: clientutils.getElementByXPath.
Here is my code:
phantom.casperPath = '..n1k0-casperjs-5428865';
phantom.injectJs(phantom.casperPath + '\\bin\\bootstrap.js');
var casper = require('casper').create();
var url = "...";
casper.start(url, function() {
casper.echo("started");
});
var x = require('casper').selectXPath;
casper.then(function()
{
casper.echo("getsid");
this.test.assertExists(x('//a[contains(#href, "home.do?SID=")]'), 'the element exists');
var element = __utils__.getElementByXPath('//a[contains(#href, "home.do?SID=")]');
});
But it fails. it returns this:
false
undefined
started
getsid
PASS the element exists <== XPATH WORKS
FAIL ReferenceError: Can't find variable: __utils__
# type: uncaughtError
# error: "ReferenceError: Can't find variable: __utils__"
ReferenceError: Can't find variable: __utils__
Try this:
phantom.casperPath = '..n1k0-casperjs-5428865';
phantom.injectJs(phantom.casperPath + '\\bin\\bootstrap.js');
var url = "...";
var casper = require('casper').create();
var x = require('casper').selectXPath;
casper.start(url, function() {
casper.echo("started");
});
casper.then(function() {
casper.echo("getsid");
var xpath = '//a[contains(#href, "home.do?SID=")]';
var xpath_arr = { type: 'xpath', path: xpath};
this.test.assertExists(xpath_arr, 'the element exists');
var element = x(xpath);
});
As pointed out in the comments, you would have to use __utils__ inside the evaluate callback, because it is injected into the page. Since you want(ed) the href, you could use:
casper.then(function(){
casper.echo("getsid");
this.test.assertExists(x('//a[contains(#href, "home.do?SID=")]'), 'the element exists');
var href = this.evaluate(function(){
var element = __utils__.getElementByXPath('//a[contains(#href, "home.do?SID=")]');
return element.href;
});
});
This can be shortened with the usage of casper.getElementAttribute:
casper.then(function(){
casper.echo("getsid");
this.test.assertExists(x('//a[contains(#href, "home.do?SID=")]'), 'the element exists');
var href = this.getElementAttribute(x('//a[contains(#href, "home.do?SID=")]'), "href");
});
You can also use casper.getElementInfo to get the complete info of the element including all the attributes (but only some properties).

Categories

Resources