java使用Selenium模拟登陆58(验证码登陆&密码登陆)若快平台识别文字点击验证码

写在前面

Selenium原本是一套网页自动化测试工具,模拟用户真实浏览操作进行测试。
如今我们可以使用Selenium进行模拟登陆,不需要像以前一样去写网络框架模拟登陆

注意:

  • 若通过一般的网络接口请求登陆足够方便,尽量使用一般的模拟登陆方式;
    Selenium由于是模拟用户实际动作进行登陆,若网站出现改版等情况很可能造成登陆失败!
  • Selenium模拟登陆比较适合用于存在js加密,ajax加密等特殊情况下;

配置环境

使用java环境+macOS+chromeDriver

  • java环境
  • ChromeDriver
  • selenium JAR

关于环境问题就自行下载了,附上下载地址:
SeleniumDownload

Selenium入门

当环境下载并且配置好之后就可以开始愉快的模拟用户登录~

  1. 创建一个ChromeDriver对象
    ChromeDriver driver = new ChromeDriver();
  2. 启动一个网址
    driver.get("http://passport.58.com/login")
  3. 运行程序

后台一串log日志之后就会发现一个chrome已经启动并且打开指定的网址了


使用Selenium登陆58

网上关于Selenium使用方法的文章很多,就不一一描述了;在这里只列举一些核心的使用方法!

·使用driver对象搜索页面中指定元素

public WebElement findElement(By by) {}

· By对象

public static By id(String id) 可绑定指定id元素

public static By linkText(String linkText) 可绑定指定的文本元素

public static By partialLinkText(String partialLinkText) 可绑定指定的连接文本

public static By name(String name) 可绑定指定name元素

public static By tagName(String tagName) 可绑定指定tag元素

public static By xpath(String xpathExpression) 可绑定指定xpath元素

public static By className(String className) 可绑定指定className元素

public static By cssSelector(String cssSelector) 可绑定指定cssSelector元素

· 根据By对象即可返回WebElement元素进行操作,常用如下:

void click() 顾名思义,点击事件

void submit() 表单提交

void sendKeys(CharSequence... keysToSend) 输入参数

String getText() 获取文本

Point getLocation() 获取相对位置

Rectangle getRect() 获取元素区域

举个栗子

  1. 使用账号密码登陆58
		driver.get("http://passport.58.com/login");

        driver.findElement(By.className("pwdlogin")).click();

        driver.findElement(By.xpath("//*[@id=\"usernameUser\"]")).sendKeys(phoneNumber);

        driver.findElement(By.xpath("//*[@id=\"passwordUserText\"]")).click();

        driver.getKeyboard().sendKeys(userPassword);

        driver.findElement(By.xpath("//*[@id=\"btnSubmitUser\"]")).click();
        
        //下文另附xpath&cssSelector快速获取方式
  1. 使用手机验证码登陆58
		driver.get("http://passport.58.com/login");
        
        driver.findElement(By.linkText("手机动态码登录")).click();
        
        driver.findElement(By.id("loginMobile")).click();//这里必须点击才能输入
        
        driver.findElement(By.id("loginMobile")).sendKeys(phoneNumber);
        
        driver.findElement(By.id("loginMobilecodeSendBtn")).click();
        
        Scanner s = new Scanner(System.in);
        
        driver.findElement(By.id("loginMobilecode")).click();//这里必须点击才能输入
        
        driver.findElement(By.id("loginMobilecode")).sendKeys(s.next());
        
        driver.findElement(By.id("loginMobileButton")).click();

若快平台识别文字点击验证码

在认证页面中需要通过文字类型的验证码,我们使用若快平台进行验证码识别
在这里插入图片描述

  1. 成功进入页面后,等待验证码加载完毕
  2. 修改当前验证码父div的大小(保证验证码保存本地时将需要验证的图片也一并保存)
  3. 本地保存验证码
  4. 将父div大小修改至默认
  5. 将验证码发至若快平台
  6. 解析数据,模拟点击
  7. 验证通过~
		WebElement elementDir = driver.findElement(By.className("isd-captcha-placeholder"));
		//假设验证码图片父div为此className

        setAttribute(elementDir, driver, "style", "width:280px;height:200px");
        //替换元素指定值(将elementDir中的style属性设置为...),方法下文另附

        WebElement element = driver.findElement(By.xpath("//*[@id=\"ISDCaptcha\"]/div/div[2]/img"));
        //验证码element

        File screenShot = element.getScreenshotAs(OutputType.FILE);

        File screenShotLocation = new File("/123.jpg");

        copyFileUsingFileStreams(screenShot, screenShotLocation);
        //保存file至本地

        Thumbnails.of("/123.jpg")
                .scale(1f)
                .outputQuality(0.5f)
                .toFile("/123.jpg");
         //压缩本地图片,由于若快平台限制上传大小,此处先做压缩;

        setAttribute(elementDir, driver, "style", "width:280px;height:150px");
        //还原验证码默认尺寸

跑通上面的代码应该就能将图片验证码保存在本地了

具体的若快平台验证流程和数据获取方式参考若快api文档

若快api文档

识别这种点击图片验证码若快的 typeID:6904

后面根据若快返回的数据在指定element上进行点击操作即可

		WebElement element = driver.findElement(By.xpath("//*[@id=\"ISDCaptcha\"]/div/div[2]/img"));

        Actions action = new Actions(driver);

        for (Point point : pointArrayList) { //若快平台返回的数据
            action.moveToElement(element, 
                    (int) (point.getX() / 2), 
                    (int) (point.getY()) / 2)
                    .click()
                    .perform();
        }

附:

By对象中的 xpath & cssSelector获取方式**

  1. 打开控制台
  2. 对指定元素右键
  3. Copy
    在这里插入图片描述

不打开浏览器在后台进行操作

//以chromedriver为例
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
        options.addArguments("--disable-gpu");

· 使用selenium修改指定element的css属性

	public static void setAttribute(WebElement e, WebDriver d, String attributeName, String value) {
        JavascriptExecutor js = (JavascriptExecutor) d;
        js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])", e, attributeName, value);
    }

自动登陆+识别gif

在这里插入图片描述

源码下载

2019-4-12测试正常,其中已经包含selenium必要的一些lib

如果无法正常使用,理解代码修改其中一些元素指定器即可

Contact me : Ronny_xie@hotmail.com

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值