zoukankan      html  css  js  c++  java
  • Selenium应用代码(常见封装的方法二)

    1. 滚动窗口:
     //将滚动条滚到适合的位置  , 方法一
          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());
                  }
         }
  • 相关阅读:
    大数据的起步:初学者
    接触区块链
    学习开始了,博客开始了
    hadoop分布式的搭建过程
    Java笔试题
    JavaSCript全局变量与局部变量
    OSGI
    restful
    jersey
    JSP+Servlet+Mybits小例子
  • 原文地址:https://www.cnblogs.com/testwang/p/5702604.html
Copyright © 2011-2022 走看看