为了账号安全,请及时绑定邮箱和手机立即绑定

封装By以后,脚本只能打开登陆网页,登陆不进去

封装By以后,脚本只能打开登陆网页,登陆不进去

qq_安晓东_lmpyGT 2018-05-15 19:22:12
package com.selenium.miyang.go;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.interactions.Actions;public class login { WebDriver driver; public void initDriver(){ System.setProperty("webdriver.chrome.driver", "D:\\wps\\selenium-2.42.2\\chromedriver.exe"); driver = new ChromeDriver(); driver.get("https://www.imooc.com/user/newlogin/from_url"); driver.manage().window().maximize(); } /* *登陆脚本 *  */ public void loginScript() throws Exception{ this.initDriver(); String username = "17729597958"; String password = "andong527011764"; String userBy ="name"; String userElement = "email"; String passBy = "name"; String passElement = "password"; String buttonBy = "className"; String buttonElement = "moco-btn"; String headerBy = "id"; String headerElement = "header-avator"; String userInforBy = "className"; String userInforElement = "name"; Thread.sleep(2000); WebElement user = this.element(this.byStr(userBy, userElement)); user.isDisplayed(); WebElement userpass = this.element(this.byStr(passBy, passElement)); userpass.isDisplayed(); WebElement login_button = this.element(this.byStr(buttonBy, buttonElement)); login_button.isDisplayed(); user.sendKeys(username); userpass.sendKeys(password); login_button.click(); Thread.sleep(2000); WebElement header = this.element(this.byStr(headerBy, headerElement)); Actions action = new Actions(driver);//鼠标悬停 action.moveToElement(header).perform(); String userInfor = this.element(this.byStr(userInforBy, userInforElement)).getText(); System.out.println(userInfor); if(userInfor.equals("慕勒2472625")){ System.out.println("登陆成功"); }else{ System.out.println("登陆失败"); } } /*封装By*/ public By byStr(String by,String local ){ if(by.equals("id")){ return By.id(local); }else if(equals("name")){ return By.name(local); }else if(by.equals("className")){ return By.className(local); }else{ return By.xpath(local); } } /*封装element*/ public WebElement element(By by){ WebElement ele=driver.findElement(by); return ele; } public static void main(String[] args) throws Exception{ login action = new login(); action.loginScript(); }}
查看完整描述

目前暂无任何回答

  • 0 回答
  • 0 关注
  • 1028 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号