【Selenium】跳轉問題


  • /** 
  •  * rewrite the get method, adding user defined log</BR> 
  •  * 地址跳轉方法,使用WebDriver原生get方法,加入失敗重試的次數定義。 
  •  *  
  •  * @param url the url you want to <span id="2_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=11&is_app=0&jk=69829344341ee6cd&k=open&k0=open&kdi0=0&luki=2&n=10&p=baidu&q=v77y4_cpr&rb=0&rs=1&seller_id=1&sid=cde61e3444938269&ssp2=1&stid=0&t=tpclicked3_hc&td=2102575&tu=u2102575&u=http%3A%2F%2Fwww%2Eylzx8%2Ecn%2Fzonghe%2Fopen%2Dsource%2F247951%2Ehtml&urlid=0" target="_blank" mpid="2" style="color: rgb(1, 70, 108); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">open</span></a></span>. 
  •  * @param actionCount retry times when load timeout occuers. 
  •  * @throws RuntimeException 
  •  */  
  • protected void get(String url, int actionCount) {  
  •     boolean inited = false;  
  •     int index = 0, timeout = 10;  
  •     while (!inited && index < actionCount){  
  •         timeout = (index == actionCount - 1) ? maxLoadTime : 10;//最后一次跳轉使用最大的默認超時時間  
  •         inited = navigateAndLoad(url, timeout);  
  •         index ++;  
  •     }  
  •     if (!inited && index == actionCount){//最終跳轉失敗則拋出運行時異常,退出運行  
  •         throw new RuntimeException("can not get the url [" + url + "] after retry " + actionCount + "times!");  
  •     }  
  • }  
  •   
  • /** 
  •  * rewrite the get method, adding user defined log</BR> 
  •  * 地址跳轉方法,使用WebDriver原生get方法,默認加載超重試【1】次。 
  •  *  
  •  * @param url the url you want to open. 
  •  * @throws RuntimeException 
  •  */  
  • protected void get(String url) {  
  •     get(url, 2);  
  • }  
  •   
  • /** 
  •  * judge if the url has navigate and page load completed.</BR> 
  •  * 跳轉到指定的URL並且返回是否跳轉完整的結果。 
  •  *  
  •  * @param url the url you want to open. 
  •  * @param timeout the timeout for page load in seconds. 
  •  * @return if page load completed. 
  •  */  
  • private boolean navigateAndLoad(String url, int timeout){  
  •     try {  
  •         driver.manage().timeouts().pageLoadTimeout(timeout, TimeUnit.SECONDS);  
  •         driver.get(url);  
  •         return true;//跳轉並且加載頁面成功在返回true  
  •     } catch (TimeoutException e) {  
  •         return false;//超時的情況下返回false  
  •     } catch (Exception e) {  
  •         failValidation();//共用的異常處理方法  
  •         LOG.error(e);//記錄錯誤日志  
  •         throw new RuntimeException(e);//拋出運行時異常,退出運行  
  •     }finally{  
  •         driver.manage().timeouts().pageLoadTimeout(maxLoadTime, TimeUnit.SECONDS);  
  •     }  
  • }  

  • 免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM