...

View Full Version : How to click a button programatically?



noamjob
12-25-2008, 01:16 PM
Here is what I need to do:

Let's say I want to make a program that perform a search by First Name in a site like this(only for demonstration, I'm not spamming...):

http://www.draftexpress.com/search.php



Let's say I want to search for "james".



How do I do it? I spent many hours on this and couldn't find out...

The main problem iis that the website is not mine, so how can I access its components?(Let's say I want to "click" the search button there programatically)?

Is there a way to do it?

I would appreciate if you expain in details, I'm preety much new in that area...



thanks,

Noam

itsallkizza
12-26-2008, 02:12 AM
First, look to see if they have an API released for public use. If they don't, then snoop around to see if they use GET instead of POST (like Google) in which case you can simulate an API.

If they are using POST for their searches, have not released an API for public use, and if their search is using any domain sandboxed code (eg any sort of ajax or server-side database calls), which it almost definitely is, then you're out of luck.

I checked out the link your posted and their search form is indeed POSTing the user's input to http://www.draftexpress.com/search.php#results so chances are you're not going to be able to do what you want using web-specific scripts. However, in principle, anything you can do manually on a computer you can also simulate. If this project is important to you, then get your hands dirty in some C/C++. There are ways to simulate browser focus and mouse/keyboard commands (in order to fill in the forms and submit) and there are plenty of screen-scraper programs out there too (to grab the data once you've got the results on screen).

However, you might try sniffing around their site to make sure they don't have an API for ya, or e-mail someone who works there and ask them if they might have what you want.

rnd me
12-26-2008, 02:46 AM
install firebug.
right-click on or next to button and select "inspect element"
find the html tag of the button. it is probably a <input type="button"> tag.
look for an onclick attrib on the tag, and copy the value of the attribute.
click the console tab of firebug and pase the code into the console.
click run, and the result should be the same as clicking the button.


if there is no onclick attrib, it's a little trickier.
you need an id for the input.
if it has one, great.
if not, while inspecting the input, right click the tag and select "new attribute", type "id" press enter and type "test1" and press enter.

click back on the console tab and type in $("test1").onclick.toString()

the code you need to run will appear in red text. copy it and paste it into a function, or run it from the console to simulate a click.

itsallkizza
12-26-2008, 03:38 AM
But if the onclick/submit function is form posting or ajax posting to a page not on his domain (in this case, on his computer) then the results won't show up... am I wrong? If you can simulate a POST on a foreign domain (afaik impossible), that's a huge security issue.

rnd me
12-26-2008, 05:53 AM
But if the onclick/submit function is form posting or ajax posting to a page not on his domain (in this case, on his computer) then the results won't show up... am I wrong? If you can simulate a POST on a foreign domain (afaik impossible), that's a huge security issue.

you can post to other domains without issue. any form can be set to any domain. some browsers will ask for a confirmation, but all allow it by default given that. noscript and other security watchdogs may not approve, but if a user is doing that, they probably know how to fix it for themselves to allow such an operation if they want.

i post to tumblr from 192.168.x.x to update my blog for instance.

i was thinking of approaching it more from a greasemonkey/userscript approach, but i suppose an http solution is possible as well, especially if you just need to build a GET string, and open the window of a search. the "not my page" thing threw me into a greasemonkey mindset...


if you need to work with the data in javascript, you will have to use a greasemonkey approach like i outline.

if you just want to view a search result page, you can easily do that by building the url, and opening a window to it, as in http://www.google.com/search?q=obama

itsallkizza
12-26-2008, 06:04 AM
you can post to other domains without issue.- I was unaware of this. I feel so dumb, I've been doing web development for 8 years now... lol

rnd me
12-26-2008, 06:33 AM
- I was unaware of this...lol
one of the best kept secrets of webdev?


it just seems like you shouldn't be able to, i know.
it weird we don't hear more about this.

the same origin policy really only applies to incoming data (except jsonp).
img pings, window.opens, and xdomain POSTs allow virtually unrestricted outbound traffic.

of course, you cannot read any cross-domain submit feedback, so javascript won't know if a POST worked or not, though you can send it to a new window/iframe for visual confirmation...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum