[twill] 'Follow' the N'th link

Kevin Boers KBoers at leapfrogonline.com
Fri Oct 10 23:52:07 PDT 2008

Hi, Jeff,

twill's follow() method just calls mechanize's find_link() interface using a few different criteria until it finds one to follow. You can pass mechanize a regex object directly, though, to return a specific one:

# Import re and twill
import re
import twill.commands as t

# Goto the target site

# Call the mechanize browser's find_link method, using the nr argument to specify which link to return
link_2 = t.browser._browser.find_link(text_regex=re.compile("Similar pages"), nr=2)
link_3 = t.browser._browser.find_link(text_regex=re.compile("Similar pages"), nr=3)

# Follow a link

There are actually several arguments that find_link will accept: text, text_regex, name, name_regex, url, url_regex, tag, predicate, nr

For more info, see the docstring for find_link at: http://codespeak.net/svn/wwwsearch/mechanize/trunk/mechanize/_mechanize.py

Hope that helps,
From: twill-bounces at lists.idyll.org [twill-bounces at lists.idyll.org] On Behalf Of Jeff Creem [jeff at thecreems.com]
Sent: Friday, October 10, 2008 10:28 PM
To: twill at lists.idyll.org
Subject: [twill] 'Follow' the N'th link

I've got a page I am dealing with where there are (sometimes) two links
on the page with the same 'name'
Is there any robust way to have the 'follow' command go to anything
other than the first link or to programmatically iterate over all of the
links from the python interface?

twill mailing list
twill at lists.idyll.org

The document(s) accompanying this e-mail transmission, if any, and the e-mail transmittal message containing information from Leapfrog Online Customer Acquisition, LLC is confidential or privileged. The information is intended to be for the use of the individual(s) or entity(ies) named on this e-mail transmission message. If you are not the intended recipient, be aware that any disclosure, copying, distribution or use of the contents of this e-mail is prohibited. If you have received this e-mail in error, please immediately delete this e-mail and notify us by telephone of the error.

More information about the twill mailing list