"""Offline Playwright Chromium verification for Bootstrap 5 Form Select & Datalist."""
import json
from playwright.sync_api import sync_playwright
CSS = """
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; font-size: 16px; line-height: 1.5; }
.wrap { width: 600px; }
.form-label { display: inline-block; margin-bottom: 0.5rem; }
.form-select {
display: block; width: 100%; padding: 0.375rem 2.25rem 0.375rem 0.75rem;
font-size: 1rem; font-weight: 400; line-height: 1.5; color: #212529;
background-color: #fff; border: 1px solid #ced4da; border-radius: 0.375rem;
appearance: none;
}
.form-select:disabled { background-color: #e9ecef; }
.form-select-lg { padding-top: 0.5rem; padding-bottom: 0.5rem; padding-left: 1rem; font-size: 1.25rem; border-radius: 0.5rem; }
.form-select-sm { padding-top: 0.25rem; padding-bottom: 0.25rem; padding-left: 0.5rem; font-size: 0.875rem; border-radius: 0.25rem; }
.form-control {
display: block; width: 100%; padding: 0.375rem 0.75rem;
font-size: 1rem; line-height: 1.5; color: #212529;
background-color: #fff; border: 1px solid #ced4da; border-radius: 0.375rem;
}
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 800, "height": 700})
# Case 1: single vs multiple form-select
page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
<div class="wrap">
<select id="single" class="form-select">
<option value="1">1</option><option value="2">2</option>
<option value="3">3</option><option value="4">4</option>
</select>
<select id="multi" multiple class="form-select">
<option value="1">1</option><option value="2">2</option>
<option value="3">3</option><option value="4">4</option>
</select>
</div></body></html>""")
page.select_option("#single", "3")
page.select_option("#multi", ["2", "4"])
act1 = page.evaluate("""() => {
const s = document.getElementById('single');
const m = document.getElementById('multi');
return {
single_width: Math.round(s.getBoundingClientRect().width),
single_appearance: getComputedStyle(s).appearance,
single_value: s.value,
multi_selected: Array.from(m.selectedOptions).map(o => o.value),
multi_taller_than_single: m.getBoundingClientRect().height > s.getBoundingClientRect().height,
};
}""")
exp1 = {
"single_width": 600,
"single_appearance": "none",
"single_value": "3",
"multi_selected": ["2", "4"],
"multi_taller_than_single": True,
}
cases.append({"name": "select_single_and_multiple", "expected": exp1, "actual": act1, "passed": act1 == exp1})
# Case 2: form-select-lg / default / form-select-sm
page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
<div class="wrap">
<select id="slg" class="form-select form-select-lg"><option>1</option></select>
<select id="smd" class="form-select"><option>1</option></select>
<select id="ssm" class="form-select form-select-sm"><option>1</option></select>
</div></body></html>""")
act2 = page.evaluate("""() => {
const cs = id => getComputedStyle(document.getElementById(id));
const h = id => Math.round(document.getElementById(id).getBoundingClientRect().height);
return {
font_sizes: [cs('slg').fontSize, cs('smd').fontSize, cs('ssm').fontSize],
heights_px: [h('slg'), h('smd'), h('ssm')],
strict_order: h('slg') > h('smd') && h('smd') > h('ssm'),
};
}""")
exp2 = {
"font_sizes": ["20px", "16px", "14px"],
"heights_px": [48, 38, 31],
"strict_order": True,
}
cases.append({"name": "select_sizing_variants", "expected": exp2, "actual": act2, "passed": act2 == exp2})
# Case 3: disabled select
page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
<div class="wrap">
<select id="sdis" class="form-select" disabled>
<option value="1">1</option><option value="2">2</option>
</select>
</div></body></html>""")
act3 = page.evaluate("""() => {
const el = document.getElementById('sdis');
return {
disabled: el.disabled,
bg: getComputedStyle(el).backgroundColor,
value: el.value,
};
}""")
exp3 = {"disabled": True, "bg": "rgb(233, 236, 239)", "value": "1"}
cases.append({"name": "select_disabled_state", "expected": exp3, "actual": act3, "passed": act3 == exp3})
# Case 4: datalist input
page.set_content(f"""<!DOCTYPE html><html><head><style>{CSS}</style></head><body>
<div class="wrap">
<label for="site" class="form-label">选择站点:</label>
<input class="form-control" list="sites" name="site" id="site">
<datalist id="sites">
<option value="MDN"></option>
<option value="Bootstrap"></option>
<option value="GitHub"></option>
<option value="Wikipedia"></option>
<option value="W3C"></option>
</datalist>
</div></body></html>""")
page.fill("#site", "MDN")
val_preset = page.input_value("#site")
page.fill("#site", "CustomSite")
act4 = page.evaluate("""(valPreset) => {
const el = document.getElementById('site');
return {
list_id: el.list ? el.list.id : null,
option_count: el.list ? el.list.options.length : 0,
preset_val: valPreset,
custom_val: el.value,
};
}""", val_preset)
exp4 = {"list_id": "sites", "option_count": 5, "preset_val": "MDN", "custom_val": "CustomSite"}
cases.append({"name": "input_datalist_binding", "expected": exp4, "actual": act4, "passed": act4 == exp4})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/bootstrap5-form-select-lab/explore-form-select.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-form-select.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a700.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a715.zip