Backend Development 8 min read

Using Python mechanize for Web Automation and Testing

The article introduces the Python mechanize library, explains its capabilities for automating web interactions such as form submission, link navigation, cookie management, and redirects, provides basic usage examples, detailed sample code for various testing scenarios, and notes its limitations compared to Selenium.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using Python mechanize for Web Automation and Testing

mechanize is a Python library that simulates a web browser, allowing automation of interactions such as filling forms, following links, handling cookies, and processing redirects. It works well for static pages but does not support JavaScript, which limits its use on modern dynamic sites.

Typical uses include automated form submission, link traversal, cookie management, redirect handling, and custom HTTP headers.

Example code:

import mechanize
# 创建一个Browser对象
br = mechanize.Browser()
# 设置浏览器的一些属性
br.set_handle_robots(False)  # 不遵守robots.txt规则
br.addheaders = [('User-agent', 'Mozilla/5.0')]  # 设置User-Agent
# 访问一个网页
response = br.open("http://example.com")
# 选择一个表单并填写数据
for form in br.forms():
if form.attrs.get('id') == 'some_form_id':  # 假设表单有一个id为'some_form_id'
br.select_form(nr=0)  # 或者使用索引选择表单
br.form['username'] = 'your_username'  # 填写用户名
br.form['password'] = 'your_password'  # 填写密码
break
# 提交表单
response = br.submit()
# 打印响应内容
print(response.read())

Although mechanize is useful for static pages, for highly dynamic sites or complex front‑end interactions you may need tools like Selenium that drive a real browser and execute JavaScript.

In automated testing, mechanize can be applied to several scenarios, each illustrated with example code:

1. Login functionality test

import mechanize
br = mechanize.Browser()
br.open("http://yourwebsite.com/login")
br.select_form(nr=0)  # 假设登录表单是第一个表单
br["username"] = "testuser"
br["password"] = "correctpassword"
response = br.submit()
if "Welcome, testuser!" in response.get_data():  # 假设登录成功会有欢迎信息
print("登录成功")
else:
print("登录失败")
# 测试错误的凭据
br.open("http://yourwebsite.com/login")
br.select_form(nr=0)
br["username"] = "testuser"
br["password"] = "wrongpassword"
response = br.submit()
if "Invalid credentials" in response.get_data():  # 假设错误信息包含"Invalid credentials"
print("错误凭据测试通过")
else:
print("错误凭据测试失败")

2. Link validity check

import mechanize
def check_links_on_page(url):
br = mechanize.Browser()
br.follow_redirects = True
br.open(url)
for link in br.links():
if link.url.startswith("http"):
try:
response = br.open(link.url)
if response.code != 200:
print(f"Link {link.url} is broken, status code: {response.code}")
except mechanize.HTTPError as e:
print(f"Error accessing {link.url}: {e}")
check_links_on_page("http://yourwebsite.com")

3. Form submission test

# 假设测试一个联系表单提交
br = mechanize.Browser()
br.open("http://yourwebsite.com/contact")
br.select_form(nr=0)
br["name"] = "Test User"
br["email"] = "[email protected]"
br["message"] = "This is a test message."
response = br.submit()
if "Thank you for your message" in response.get_data():  # 假设成功提交会有感谢信息
print("表单提交成功")
else:
print("表单提交失败")

4. Cookie handling test

br = mechanize.Browser()
br.open("http://yourwebsite.com/setcookie")
# 假设访问此页面会设置cookie
print("Initial cookies:", br.cookiejar)
br.open("http://yourwebsite.com/cookiepage")  # 假设此页面读取并显示cookie
response_text = response.read()
if "Cookie value here" in response_text:  # 替换为实际cookie值的检查
print("Cookie handling test passed")
else:
print("Cookie handling test failed")

5. Redirect test

br = mechanize.Browser()
br.open("http://yourwebsite.com/login")
br.select_form(nr=0)
br["username"] = "testuser"
br["password"] = "correctpassword"
response = br.submit()
final_url = response.geturl()
expected_url = "http://yourwebsite.com/dashboard"  # 登录后预期的URL
if final_url == expected_url:
print("Redirect after login test passed")
else:
print(f"Redirect test failed. Expected: {expected_url}, got: {final_url}")

Adjust the example code to match the specific structure and requirements of your target website, and consider using Selenium for scenarios that require full JavaScript support.

httpSeleniumweb testingcookiesWeb Automationmechanize
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.