Hi,

I am trying to use the 'find' command of twill from within a python code but it seems to return no result. I tried to run that command on the command prompt by running twill and it works fine. But when I try to run the same command from a python code, it returns 'None' as result.

My code is as follows:

-- trial.py--
from twill.commands import *
from twill import commands

go('http://www.gmail.com/')
showforms()
print find("Email")

This returns 'None' on the command prompt when I run from the command prompt.

Any help or suggestions will be very helpful. Thank you.

Dani AI

Generated

Short, practical clarification and a couple of fixes for the two issues raised by (and the “find in what?” pointer from ).

Twill’s command functions are primarily the interactive twill language — many of those wrappers print output and do not return usable Python objects. For programmatic use, inspect the browser object (properties like html, url, forms, code) instead of relying on command return values. The docs show this pattern and also note you can capture command output with twill.set_output(...). (twill.readthedocs.io)

Practical patterns (use these from a script, not the interactive shell):

  • Read the page HTML and search it with Python (safer and reliable across twill versions):

    from twill import browser
    import re
    
    browser.go("https://example.com/check_password")
    html = browser.html
    if re.search(r"Email", html, re.I):
        print("found")
  • If a command prints useful text, capture it:

    import io, twill
    buf = io.StringIO()
    twill.set_output(buf)
    from twill.commands import showforms
    showforms()
    out = buf.getvalue()

Twill does not execute JavaScript. It does handle HTTP redirects and meta-refresh, but JS-driven redirects (e.g. window.location = ..., JS submit handlers) will not run — that explains why twill “stops” on the check_password page. Two realistic options: (a) parse the returned HTML/inline JS for the target URL and call browser.go(...) yourself, or (b) use a JS-capable driver such as Selenium or Playwright to let the page run its scripts. (twill.readthedocs.io)

Quick troubleshooting checklist: verify the exact HTML in browser.html, check browser.url and browser.code, try a browser-like user-agent, and if the site relies on client-side JS for navigation or auth, switch to a real browser automation tool. (twill.readthedocs.io)

Recommended Answers

All 2 Replies

I know nothing about twill, but find "EMail" in what? You would also have to give it a file name or a string, etc. to search in. You would want to tell it if you want a search or a match as well. Looks like Google time. Also, see if Twill has a forum or mailing list that you can search.

Hi,

I encountered another problem while using twill from command prompt. I have a website which uses a "check_password" page to check the user password entered for login. If the password is correct, the user is automatically redirected to the home page of the site. This works fine in any standard browser, IE, Firefox, Opera... But while using twill, twill stops at the "check_password" page. This page contains no forms, no links, nothing. It just includes some javascript files and calls a function which is used for redirecting the browser. I dont have the source code for the javascript files. I know that a javascript function is called because of the "show" command that I used from twill command prompt, which showed the html of the current "check_password" page.

I checked with another site that used such "check_password" page and twill stopped at that page too. I dont have the source codes for these sites. I am simply using twill as a text browser to view them.

Any workarounds for such a problem ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.