【Selenium】跳转问题

  1. /** 
  2.  * rewrite the get method, adding user defined log</BR> 
  3.  * 地址跳转方法,使用WebDriver原生get方法,加入失败重试的次数定义。 
  4.  *  
  5.  * @param url the url you want to <span id="2_nwp" style=" 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);  auto; height: auto;">open</span></a></span>. 
  6.  * @param actionCount retry times when load timeout occuers. 
  7.  * @throws RuntimeException 
  8.  */  
  9. protected void get(String url, int actionCount) {  
  10.     boolean inited = false;  
  11.     int index = 0, timeout = 10;  
  12.     while (!inited && index < actionCount){  
  13.         timeout = (index == actionCount - 1) ? maxLoadTime : 10;//最后一次跳转使用最大的默认超时时间  
  14.         inited = navigateAndLoad(url, timeout);  
  15.         index ++;  
  16.     }  
  17.     if (!inited && index == actionCount){//最终跳转失败则抛出运行时异常,退出运行  
  18.         throw new RuntimeException("can not get the url [" + url + "] after retry " + actionCount + "times!");  
  19.     }  
  20. }  
  21.   
  22. /** 
  23.  * rewrite the get method, adding user defined log</BR> 
  24.  * 地址跳转方法,使用WebDriver原生get方法,默认加载超重试【1】次。 
  25.  *  
  26.  * @param url the url you want to open. 
  27.  * @throws RuntimeException 
  28.  */  
  29. protected void get(String url) {  
  30.     get(url, 2);  
  31. }  
  32.   
  33. /** 
  34.  * judge if the url has navigate and page load completed.</BR> 
  35.  * 跳转到指定的URL并且返回是否跳转完整的结果。 
  36.  *  
  37.  * @param url the url you want to open. 
  38.  * @param timeout the timeout for page load in seconds. 
  39.  * @return if page load completed. 
  40.  */  
  41. private boolean navigateAndLoad(String url, int timeout){  
  42.     try {  
  43.         driver.manage().timeouts().pageLoadTimeout(timeout, TimeUnit.SECONDS);  
  44.         driver.get(url);  
  45.         return true;//跳转并且加载页面成功在返回true  
  46.     } catch (TimeoutException e) {  
  47.         return false;//超时的情况下返回false  
  48.     } catch (Exception e) {  
  49.         failValidation();//共用的异常处理方法  
  50.         LOG.error(e);//记录错误日志  
  51.         throw new RuntimeException(e);//抛出运行时异常,退出运行  
  52.     }finally{  
  53.         driver.manage().timeouts().pageLoadTimeout(maxLoadTime, TimeUnit.SECONDS);  
  54.     }  
  55. }  
原文地址:https://www.cnblogs.com/baoyu7yi/p/7137679.html