Selenium应用代码(常见封装的方法二)
2024-09-18 05:32:59
- 滚动窗口:
//将滚动条滚到适合的位置 , 方法一
public static void setScroll(WebDriver driver,int height){
try { // String setscroll = "document.documentElement.scrollTop=" + height;
String setscroll = "document.body.scrollTop=" + height;
JavascriptExecutor jse=(JavascriptExecutor) driver;
jse.executeScript(setscroll); } catch (Exception e) {
System.out.println("Fail to set the scroll.");
}
}
//将滚动条滚到适合的位置 , 方法二
public static void setScroll2(WebDriver driver,By by){
try {
WebElement page2 = driver.findElement(by);
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].scrollIntoView()", page2); } catch (Exception e1) {
System.out.println("Fail to set the scroll.");
}
}
//将滚动条滚到适合的位置 , 方法三
public static void scrollToElement(WebDriver driver,By by) {
WebElement e = driver.findElement(by);
logger.info("scroll view element");
JavascriptExecutor js = (JavascriptExecutor) driver;
// roll down and keep the element to the center of browser
js.executeScript("arguments[0].scrollIntoViewIfNeeded(true);", e);
} //滚动到目标元素的纵坐标位置(Link),点击 public void scrollAndClick(WebDriver driver,By by)
{
WebElement element = driver.findElement(by);
int elementPosition = element.getLocation().getY();
String js = String.format("window.scroll(0, %s)", elementPosition);
((JavascriptExecutor)driver).executeScript(js);
element.click();
}
2. 判断web链接返回状态是否为2开头
// 封装判断web链接返回状态是否为2开头的
public static void ReadUrl(String surl){
try {
URL url = new URL(surl);
URLConnection rulConnection = url.openConnection();
HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection;
httpUrlConnection.setConnectTimeout(300000);
httpUrlConnection.setReadTimeout(300000);
httpUrlConnection.connect();
String code = new Integer(httpUrlConnection.getResponseCode()).toString();
String message = httpUrlConnection.getResponseMessage();
System.out.println("getResponseCode code ="+ code);
System.out.println("getResponseMessage message ="+ message);
if(!code.startsWith("2")){
throw new Exception("ResponseCode is not begin with 2,code="+code);
}
// 打印链接返回状态码
// System.out.println(getDateTime()+"连接"+surl+"正常");
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
最新文章
- c#批量插入数据库Demo
- [Scala] Scala基础知识
- category用法
- mysql 5.6.34 二进制
- jquery easyui的treegrid的控制
- jQuery 操作复选框(checkbox) attr checked不起作用
- 防止apache下面直接输入目录访问文件
- 关于hbase的read操作的深入研究 region到storefile过程
- [SCOI2009]游戏
- 淘宝开源Web服务器Tengine安装教程
- nyoj 757 期末考试【优先队列+贪心】
- Linux学习笔记5——虚拟内存
- UVALive - 3263 That Nice Euler Circuit (几何)
- cordova.js的坑
- 通用的Android控件抖动效果实现
- rpm和yum软件管理(week2_day5)--技术流ken
- 编译器处理警告、错误 #pragma GCC diagnostic ignored ";-Wunused";
- javascript判断两个对象属性以及值是否相等
- linux下搭建我的世界spongeforge 服务器 (海绵端)
- Server runtime