//元素[@属性=’属性值‘] | |||||||||
// | 从当前节点出发遍历整个文档 /body/div/div/div[@class='login-button']
| ||||||||
---|---|---|---|---|---|---|---|---|---|
元素 | div/input/... | ||||||||
[@=''] | @选取属性 & ''属性的值 this.username_input_xpath = "//input[@placeholder='请输入手机号']";
| ||||||||
[text()=’‘] | 匹配元素的文本内容,一般用于
this.pwd_way_xpath = "//div[text()='账号登录']"// |
填入账号密码的处理对比
//登录页 class LoginPage { constructor(page) { this.title = "Login Page"; this.page = page; this.login_button_xpath = "//div[@class='login-button']";//登录按钮 找到页面中所有 class 为 login-button 的 div 元素。 this.username_input_xpath = "//input[@placeholder='请输入手机号']";//用户名输入框 this.password_input_xpath = "//input[@placeholder='密码为数字、英文、符号的组合']"; this.pwd_way_xpath = "//div[text()='账号登录']"// } async goto(url) { await this.page.goto(url) } async clickLoginButton() { await this.page.locator(this.login_button_xpath).click() } async clickPwdWay() { await this.page.locator(this.pwd_way_xpath).click() } async inputUsername(username) { await this.page.locator(this.username_input_xpath).fill(username) } async inputPassword(password) { await this.page.locator(this.password_input_xpath).fill(password) } } module.exports = LoginPage; | //创建新页面 //输入账号密码 await page.fill('input[placeholder="请输入手机号"]', mobile); await page.fill('input[type="password"]', password); //或者 placeholder="密码为数字、英文、符号的组合" |
---|---|
高维护性,易复用,适合复杂场景 | 快速实现,适合简单场景(不考虑密码明文泄露的情况,缺陷是当kookpc端登录的时候会首先触发快捷登录页面,无法跳转到密码登录页面) |