Day 3:定位策略进阶 适用人群掌握基础定位想应对更复杂的页面结构预计耗时10 分钟目标学会 CSS 选择器、XPath、getByRole 等进阶定位方法今天学到的定位方法定位方法语法示例适用场景getBylocator#divWithBeforePseuodoEl / //div[contains(text(),‘Inner’)]CSS 选择器或 XPathgetBylocatorNthspangetByTextExactspannn精确文字匹配getByRoleByCheckBoxabcd按角色定位 checkboxgetByRoleByRadioKey Up按角色定位 radiogetByPlaceholderEnter Email占位符定位上传并执行 day03_step.xlsx文件包含 2 个用例。spandiv 用例页面spandiv.htm包含各种 span 和 div 结构步骤定位方式定位值说明CSS 定位getBylocator#divWithBeforePseuodoElCSS ID 选择器XPathgetBylocator//div[contains(text(),‘Inner’)]XPath 层级定位第 N 个getBylocatorNthspan精确文本存储getByTextExact → 存储变量spannn → actualText取文本并存为变量断言验证assertactualTextcheckbox 用例页面label.htm keypress.htm包含标签化复选框、单选框和占位符输入框步骤定位方式定位值说明打开表单页navigateTolabel.htm带标签和占位符的页面勾选复选框getByRoleByCheckBoxabcd按角色定位 checkbox输入搜索框getByPlaceholderEnter Email按 placeholder 定位打开单选页navigateTokeypress.htm切换页面选中单选框getByRoleByRadioKey Up按角色定位 radiogetBylocator 详解getBylocator 是最灵活的定位方式支持 CSS 和 XPath 两种语法。CSS 选择器示例写法含义#btnIdid 为 btnId 的元素.btnClassclass 为 btnClassdiv spandiv 的直接子元素 spanXPath 示例写法含义//div[classx]所有 classx 的 div//input[nameusername]nameusername 的 input//button[contains(text(),Submit)]文字含 Submit 的按钮N 后缀定位方法说明getBylocatorNthselectorgetByFirstlocator第一个元素getByLastlocator最后一个元素动手练习用 getBylocator 写一个 XPath 选中 spandiv 页面中的 inner 元素用 getByRoleByCheckBox 在 label.htm 页面勾选some bold text复选框用 getByPlaceholder 定位 label.htm 页面的Enter Password输入框执行结果下一篇预告Day 4 — 元素操作基础