selenium IDE下载及使用
原文链接https://blog.csdn.net/lj606/article/details/115003131
selenium IDE结合浏览器提供脚本的录制、回放以、编辑脚本功能、以及元素的定位,可以使用selenium IDE将录制的脚本生成相应的带单元测试框架的自动化测试脚本。文章来源:https://www.toymoban.com/news/detail-737647.html
目标
自动登录某带有人机验证网站,并获取cookie文章来源地址https://www.toymoban.com/news/detail-737647.html
1. 引入selenium包
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.3.1</version>
</dependency>
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<String, Object> vars;
JavascriptExecutor js;
// @Before
// public void setUp() {
// driver = new ChromeDriver();
// js = (JavascriptExecutor) driver;
// vars = new HashMap<String, Object>();
// }
// @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<String, Object> vars = new HashMap<String, Object>();
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<Integer> 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<Integer> getTracks(int distance) {
double rate = 0.6;
double t = 0.2;
double v = 0;
List<Integer> list = new ArrayList<>();
double mid = rate * distance;
int s = 0;
while (s<distance) {
//初始速度
double v0 = v;
double a = s<mid? 20:-3;
//计算当前t时间段走的距离
double s0 = v0 * t + 0.5 * a * t * t;
// 计算当前速度
v = v0 + a * t;
// 四舍五入距离,因为像素没有小数
list.add((int) s0);
// 计算当前距离
s += s0;
}
return list;
}
}
3. 效果
到了这里,关于JAVA+Selenium实现滑块验证的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!