Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21748

Re: Python simulate browser activity

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Python simulate browser activity
Date 2012-03-16 08:57 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-2C3F24.08573016032012@news.panix.com> (permalink)
References <214c4c0c-f8ec-4030-946b-8becc8e1aa9c@ur9g2000pbc.googlegroups.com>

Show all headers | View raw


In article 
<214c4c0c-f8ec-4030-946b-8becc8e1aa9c@ur9g2000pbc.googlegroups.com>,
 choi2k <rex.0510@gmail.com> wrote:

> Hi, everyone
> 
> I am trying to write a small application using python but I am not
> sure whether it is possible to do so..
> The application aims to simulate user activity including visit a
> website and perform some interactive actions (click on the menu,
> submit a form, redirect to another pages...etc)
> I have found some libraries / plugins which aims to simulate browser
> activity but ... none of them support AJAX request and/or running
> javascript.
> 
> Here is one of the simple tasks which I would like to do using the
> application:
> 1. simulate the user activity, visit the website ("https://
> www.abc.com")
> 2. find out the target element by id ("submitBtn") and simulate mouse
> click on the item.
> 3. perform some javascript functions which invoked by click event from
> step 2. ( the function is bind to the item by jquery)
> 4. post ajax request using javascript and if the page has been
> redirected, load the new page content
> 
> Is it possible to do so?
> 
> Thank you in advance.

Depending on exactly what you're trying to test, this may or may not be 
possible.

#1 is easy.  Just get the URL with urllib (or, even better, Kenneth 
Reitz's  very cool requests library (http://docs.python-requests.org/).

#2 gets a little more interesting, but still not that big a deal.  Parse 
the HTML you get back with something like lxml (http://lxml.de/).  
Navigate the DOM with a CSSSelector to find the element you're looking 
for.  Use urllib/requests again to get the href.

#3 is where things get fun.  What, exactly, are you trying to test?  Are 
you trying to test that the js works, or are you really trying to test 
your server and you're just using the embedded js as a means to generate 
the next HTTP request?

If the former, then you really want to be exploring something like 
selenium (http://seleniumhq.org/).  If the latter, then skip all the js 
crap and just go a GET or POST (back to urllib/requests) on the 
appropriate url.

#4 falls into the same bucket as #3.

Once you have figured all this out, you will get a greater appreciation 
for the need to cleanly separate your presentation (HTML, CSS, 
Javascript) from your business logic and data layers.  If there is a 
clean interface between them, it becomes easy to test one in isolation 
from the other.  If not, then you end up playing games with screen 
scraping via lxml and selenium just to test your database queries.  
Which will quickly lead to you running screaming into the night.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python simulate browser activity choi2k <rex.0510@gmail.com> - 2012-03-15 19:23 -0700
  Re: Python simulate browser activity Chris Rebert <clp2@rebertia.com> - 2012-03-15 19:54 -0700
  Re: Python simulate browser activity Jerry Hill <malaclypse2@gmail.com> - 2012-03-15 23:44 -0400
  Re: Python simulate browser activity Roy Smith <roy@panix.com> - 2012-03-16 08:57 -0400
  Re: Python simulate browser activity Chris Angelico <rosuav@gmail.com> - 2012-03-17 06:20 +1100
  Re: Python simulate browser activity NA <NA> - 2012-03-16 21:51 -0400

csiph-web