Website button click - Perl WWW::Mechanize - html

I try to use the perl script to automate the interaction with a website.
I use module WWW::Mechanize to realize my design. But I cannot perform the button click in my perl script by using command as below.
$mech->click( $button [, $x, $y] )
$mech->click_button( ... )
It is because this button does not belongs to any form and without name, I can not call or locate this button in my perl script. How can I make it possible to click this button in my perl script like I interact with browser? Thank you.
<button class="button transactional" id="checkout-now"><span>Check Out Now</span></button>

If button does not belong to any form it should have onclick event defined, like #Bilzac and #hijack mentioned. In this case you are not able to reproduce browser's behavior because WWW::Mechanize does only html analysis.
Dealing with JavaScript events it's more easy to implement browser's network activity rather then implementing whole JavaScript events and DOM interaction.

Well sometimes all you need is $mech->post() because it's harder to find what going on with JavaScript when you click some element.
So you need to find what request is performed when you click this button (you may use Firefox HttpFox for this) and after that construct same request using WWW::Mechanize:
$mech->post($request_url, Content => {FORM_FIELDS...});

You can add onclick event on this button. like this:
<button onlick="alert('hello');">Click Me</button>

Clicking the button in a browser only runs a piece of JavaScript. You can't do that in WWW::Mechanize, because it has no JavaScript support.
What you could do, however, is find the JavaScript code that's being run when the button is clicked and write some Perl code to do the same thing. Unfortunately, it can sometimes be hard to find which JavaScript event handlers apply to an element. Even the otherwise excellent Firebug doesn't seem to have any simple way to list all event handlers associated with an element. (There does exist an Eventbug extension, but at a glance it doesn't seem to work very well.) The Visual Event bookmarklet might also be worth trying.

I am not 100% sure what you are asking for, but I am going to swing at it.
You do not need the button to be part of a form for any sort of interactivity. You can do this with just JavaScript & HTML.
$('#checkout-now').click( function() {
alert('Hello!');
//Do other stuff here, regarding the form! (Call Perl scripts etc.)
});
http://jsfiddle.net/fCdxR/1/
Quick example of how it works!
Hope this solves your problem.

Related

Modify DOM in content-script as a replacement for the non-ability to trigger pop-ups programmatically?

I'm working on an extension that's supposed to use the content of the page to determine whether to show an interface to the user.
The ways to show an interface, if I'm correct, are using a browser action or a page action.
And neither can be triggered programmatically. But content scripts could be written to inject an equivalent GUI into the webpage.
So, does it make sense to modify the DOM using content-scripts to display an interface as a substitute for page action? It seems like an obvious work around to me, and I'm sure there are good reasons to not let page actions be triggered programmatically.
Well, modifying DOM must be done by only Content Scripts, as that is the reason they exist.
Want to fetch any data from current page, alter anything in the page, add new UI in the page - whatever, content script will help you do that.
It has nothing to do with Page script Or Browser Script.
YES, you can not programatically trigger page/browser action. It has to be done by explicit clicking.
But if you want to open a UI by clicking a chrome extension, then there is a popup js for that.

Is there a way to see helpful information regarding event listeners in chrome/firefox developer tools?

Since I am binding to a click event with jQuery, it is really hard to figure out where that code is in my application.
The event listeners panel in chrome looks like this:
However, when clicking on the definition, I am taken to jquery. There is no way to figure out which function was sent to jQuery, even if I blackbox the jquery script.
In firefox developer edition is see something similar:

handling button onclick events using selenium webdriver

I have html code for button like below
<button class="btn btn-mini" onclick="setLive()">
I would like to write a selenium script which could check whether the button is clicked or not. could someone tell me how could I write a script in selenium webdriver.
I don't believe that Selenium can actually tell whether a button has been clicked as that information is visible only to the browser itself. Selenium is designed to attempt actions through the browser interface and then validate the results. The behavior you want does not fall within this concept. The proper thing to do here is not attempt to figure out if the browser has detected a button click, but to validate the expected results upon clicking the button (whatever those user-visible results may be). That's the proper way to test application behavior. Verifying that a button has successfully sent a click signal to the browser process won't help you if the expected results don't manifest.
As for how to do that... you're going to have to do some reading and start learning Java, C#, Python, or one of the other myriad languages that the Selenium library supports.
onClick() events not gets executed when you just perform click() function on WebElement.
You should try this:
WebElement btn=driver.findElement(By.className("btn btn-mini"));
JavascriptExecuter jse=(JavascriptExecuter)driver;
jse.executeScript("arguments[0].click();",btn);

HTML5 - When button is pressed open a window with options

At the moment i have a normal button:
Sign up
Instead of sending it to a link immediatly i want it to display a popup window with mutliple options like this:
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_prompt
But instead of the textbox i would like it to display checkboxes with the site it will direct you to. (and you can only select one checkbox)
Quick example i made in paint:
http://oi57.tinypic.com/s5cvu1.jpg
Not sure if this is possible since i'm not experienced with working on websites.
Your question is very broad and I don't think SO is here to give you a tutorial but as a help for you to get started I suggest you check out window.open event that you will need to fire in order to open a pop up http://www.w3schools.com/jsref/met_win_open.asp then on the pop up window you will have to decide whether it's better to have a checkbox or a radiobutton, since you want that only one option is ticked and so on. I suggest you look on google for "pop ups getting started" and javascript. I hope that helps.
Use onclick method:
<a href="" class="button" onclick=Login(); >Sign up</a>
again define the method signup in Scrpt tag
<script>
Login()
{
}
</script>
For better understanding refer this example:
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick
This is very simple example, alternatively, if u r aware of jquery, it will make your work very easy, going for jquery is better option.
http://jqueryui.com/dialog/

Dynamic html onClick

So I am playing around with the HTML service of GAS.
It has been working fine (more or less). But I hit a wall now.
I want the script (inside the html file) to create some html on when called.
In particular a few tags.
However, I want those tags to have onClick handlers (which will edit the div element).
Now the problem is that the onClick should depend on certain properties and I can not
pre-create those objects, they have to be made pragmatically and for some reason when I add a
onClick="someFunction(elementID)" after the new code has been added to the old one the click handler disappears.
(it works tho if the handler function has no parameters)
eg.
var div="<div id=\"box"+count+"\" class=\"square\" insert></div>";
if(something)
div=div.replace("insert", "onclick=\"myFunction(box"+count+"\"");
This is intentional and documented: see the section called "Dynamically adding scripts or external CSS" in the HtmlService user guide.