Selenium原本是一套网页自动化测试工具,模拟用户真实浏览操作进行测试。
如今我们可以使用Selenium进行模拟登陆,不需要像以前一样去写网络框架模拟登陆
注意:
使用java环境+macOS+chromeDriver
关于环境问题就自行下载了,附上下载地址:
SeleniumDownload
当环境下载并且配置好之后就可以开始愉快的模拟用户登录~
ChromeDriver driver = new ChromeDriver();
driver.get("http://passport.58.com/login")
后台一串log日志之后就会发现一个chrome已经启动并且打开指定的网址了
网上关于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()
获取元素区域
举个栗子
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快速获取方式
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();
在认证页面中需要通过文字类型的验证码,我们使用若快平台进行验证码识别
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();
}
//以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);
}
2019-4-12测试正常,其中已经包含selenium必要的一些lib
如果无法正常使用,理解代码修改其中一些元素指定器即可
Contact me : [email protected]