• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

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

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();
               
               
        }
}


免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

发新帖

发布任务需求已有1031167位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!