JAVA+Selenium实现滑块验证

selenium IDE下载及使用

原文链接https://blog.csdn.net/lj606/article/details/115003131

selenium IDE结合浏览器提供脚本的录制、回放以、编辑脚本功能、以及元素的定位,可以使用selenium IDE将录制的脚本生成相应的带单元测试框架的自动化测试脚本。

目标

自动登录某带有人机验证网站,并获取cookie

1. 引入selenium包


			org.seleniumhq.selenium
			selenium-java
			3.3.1
		

2. 代码

package com.shuidihuzhu.cs.workorder.service.crawler;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author by zhangleishuidihuzhu.com
 * @Date 2022/11/22 17:01
 */
@Slf4j
public class CrawlerServiceSeleniumImpl implements CrawlerService {

    private static WebDriver driver;
    private Map vars;
    JavascriptExecutor js;

//    @Before
//    public void setUp() {
//        driver = new ChromeDriver();
//        js = (JavascriptExecutor) driver;
//        vars = new HashMap();
//    }
//    @After
//    public void tearDown() {
//        driver.quit();
//    }
    @Override
    public void spiderPages() {

    }


    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new ChromeDriver();
        JavascriptExecutor js = (JavascriptExecutor) driver;

        Map vars = new HashMap();
        String url = "https://www.yqt365.com/staticweb/#/login";
        driver.get("https://www.yqt365.com/staticweb/#/login");
        driver.manage().window().setSize(new Dimension(1440, 797));
        driver.findElement(By.xpath("/html/body/app-root/layout-login/div/app-routing-login/app-login-enter/div[1]/div/div[2]/div[2]/div[2]/form/nz-form-item[1]/nz-form-control/div/span/nz-input-group/input")).sendKeys("shuidi");
        driver.findElement(By.xpath("/html/body/app-root/layout-login/div/app-routing-login/app-login-enter/div[1]/div/div[2]/div[2]/div[2]/form/nz-form-item[2]/nz-form-control/div/span/nz-input-group/input")).sendKeys("Sina2021");
        driver.findElement(By.cssSelector(".geetest_wait")).click();

        try {
            //等一下,等滑动图片出来
            Thread.sleep(4000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        driver.switchTo().frame(1);
        WebElement block = driver.findElement(By.xpath("/html/body/div[3]/div[2]/div[2]/div[1]/div[2]/div[2]"));
        WebElement reload = driver.findElement(By.xpath("/html/body/div[3]/div[2]/div[2]/div[2]/div/a[2]"));

        int i = 1;
        int j = 1;
        while (true) {
            try {
                Actions actions = new Actions(driver);
                actions.clickAndHold(block).perform();
                actions.moveByOffset(80,0).perform();
                List tracks = getTracks(30);

                log.info("第 {} 轮, 第 {} 次移动", i, j++);
                for (Integer track : tracks) {
                    actions.moveByOffset(track, 0).perform();
                }

                actions.release().perform();

                Thread.sleep(2000L);

                if(!driver.getCurrentUrl().equals("https://www.yqt365.com/staticweb/#/login")) {
                    System.out.println("登录成功");
                    break;
                }else {
                    reload.click();
                    Thread.sleep(2000L);
                }
            } catch (Exception e) {
                log.info("刷新验证码次数过多,出现校验,点击");
                WebElement retry = driver.findElement(By.xpath("//*[@id=\"jiyanCheck\"]/div/div[2]/div[1]/div[3]/span[2]"));
                if(retry != null) {
                    retry.click();
                    Thread.sleep(5000L);
                }
                i++;
            }
        }



        driver.findElement(By.cssSelector(".geetest_slider_button")).click();
        driver.findElement(By.cssSelector(".login-form-button")).click();
        {
            WebElement element = driver.findElement(By.cssSelector(".login-form-button"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        {
            WebElement element = driver.findElement(By.tagName("body"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element, 0, 0).perform();
        }
        driver.findElement(By.cssSelector(".content")).click();


        driver.quit();
    }

    private static List getTracks(int distance) {
        double rate = 0.6;
        double t = 0.2;
        double v = 0;
        List list = new ArrayList<>();
        double mid = rate * distance;

        int s = 0;
        while (s

3. 效果

你可能感兴趣的:(java学习笔记,java,selenium,前端)