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="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>.
  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. * rewrite the get method, adding user defined log</BR>
  23. * 地址跳转方法,使用WebDriver原生get方法,默认加载超重试【1】次。
  24. *
  25. * @param url the url you want to open.
  26. * @throws RuntimeException
  27. */
  28. protected void get(String url) {
  29. get(url, 2);
  30. }
  31. /**
  32. * judge if the url has navigate and page load completed.</BR>
  33. * 跳转到指定的URL并且返回是否跳转完整的结果。
  34. *
  35. * @param url the url you want to open.
  36. * @param timeout the timeout for page load in seconds.
  37. * @return if page load completed.
  38. */
  39. private boolean navigateAndLoad(String url, int timeout){
  40. try {
  41. driver.manage().timeouts().pageLoadTimeout(timeout, TimeUnit.SECONDS);
  42. driver.get(url);
  43. return true;//跳转并且加载页面成功在返回true
  44. } catch (TimeoutException e) {
  45. return false;//超时的情况下返回false
  46. } catch (Exception e) {
  47. failValidation();//共用的异常处理方法
  48. LOG.error(e);//记录错误日志
  49. throw new RuntimeException(e);//抛出运行时异常,退出运行
  50. }finally{
  51. driver.manage().timeouts().pageLoadTimeout(maxLoadTime, TimeUnit.SECONDS);
  52. }
  53. }

【Selenium】跳转问题的更多相关文章

  1. selenium跳过webdriver检测并爬取淘宝我已购买的宝贝数据

    简介 上一个博文已经讲述了如何使用selenium跳过webdriver检测并爬取天猫商品数据,所以在此不再详细讲,有需要思路的可以查看另外一篇博文. 源代码 # -*- coding: utf-8 ...

  2. selenium跳过webdriver检测并爬取天猫商品数据

    目录 简介 编写思路 使用教程 演示图片 源代码 @(文章目录) 简介 现在爬取淘宝,天猫商品数据都是需要首先进行登录的.上一节我们已经完成了模拟登录淘宝的步骤,所以在此不详细讲如何模拟登录淘宝.把关 ...

  3. selenium跳过webdriver检测并模拟登录淘宝

    目录 简介 编写思路 使用教程 演示图片 源代码 @(文章目录) 简介 模拟登录淘宝已经不是一件新鲜的事情了,过去我曾经使用get/post方式进行爬虫,同时也加入IP代理池进行跳过检验,但随着大型网 ...

  4. selenium跳过https的问题

    背景: 周六产品给我反馈:支付成功页面后会提示这个,问自动化为什么没有发现这样的问题 第一反应:这个地址肯定被举报了,我也肯定没有设置过安全链接,因为都没有见过这样的网址,如果有问题,应该会直接出错, ...

  5. Scrapy——5 下载中间件常用函数、scrapy怎么对接selenium、常用的Setting内置设置有哪些

    Scrapy——5 下载中间件常用的函数 Scrapy怎样对接selenium 常用的setting内置设置 对接selenium实战 (Downloader Middleware)下载中间件常用函数 ...

  6. python爬虫10 b站爬取使用 selenium+ phantomJS

    但有时候 我们不想要让它打开浏览器去执行 能不能直接在代码里面运行呢 也就是说 有没有一个无形的浏览器呢 恩 phantomJS 就是 它是一个基于 WebKit 的浏览器引擎 可以做到无声无息的操作 ...

  7. Python+Selenium - windows安全中心的弹窗(账号登录)

    当出现如下图所示的 Windows安全中心弹窗,需要输入用户名和密码时 如何用Python+selenium跳过这个登录. 步骤: 1.在注册表中三个位置各添加两个东西:iexplore.exe 和 ...

  8. python爬虫11 | 这次,将带你爬取b站上的NBA形象大使蔡徐坤和他的球友们

    在上一篇中 python爬虫10 | 网站维护人员:真的求求你们了,不要再来爬取了!! 小帅b给大家透露了我们这篇要说的牛逼利器 selenium + phantomjs 如果你看了 python爬虫 ...

  9. selenium如何跳转到iframe

    处理iframe,selenium提供了3中方法: 1.html的frame有id,传入id 2.html的frame有name,传入name 3.先找到iframe的元素,再传入该元素对象 跳出if ...

  10. selenium+python 浏览器标签页跳转 switch_to_window

    浏览器页面跳转方法记录: from selenium import webdriver import time browser = webdriver.Chrome() first_url='http ...

随机推荐

  1. Parameter Binding in ASP.NET Web API #Reprinted

    http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

  2. 微信小程序 压缩图片并上传

    转自https://segmentfault.com/q/1010000012507519 wxml写入 <view bindtap='uploadImg'>上传</view> ...

  3. InnoDB: Warning: a long semaphore wait 解决办法

    http://blog.csdn.net/wulantian/article/details/37560849

  4. filter和spring 的interceptor都是单例的,都不是线程安全的

    Filter 是在 Servlet 容器启动时就初始化的,因此可以认为是以单例对象存在的,如果一个请求线程对其中的成员变量修改的话,会影响到其他的请求线程,因此认为是多线程不安全的.

  5. MySQL常用SQL整理

    MySQL常用SQL整理 一.DDL #创建数据库 CREATE DATABASE IF NOT EXISTS product DEFAULT CHARSET utf8 COLLATE utf8_ge ...

  6. Java中char转为16进制

    Java中char转为16进制 char a = '0'; String hexStr = Integer.toHexString(a); System.out.println(hexStr);

  7. Odoo超售订单

    当 交付给客户的货物多于订购的数量时,就形成'超售'状态: 对于超售的部分,需要进行开票处理,以及根据情况修改交货     发生超售的前提是,产品开票策略为 '按订购数量开票'     同时需要 允许 ...

  8. C语言-回溯例4

    1,问题提出 日本数学家桥本吉彦教授于1993年10月在我国山东举行的中日美三国数学教育研讨会上向与会者提出以下填数趣题: 把1,2,...,9这9个数字填入下式的九个方格中(数字不得重复),使下面 ...

  9. Selenium系列之--08 操作已打开的浏览器

    Can Selenium interact with an existing browser session? 参考上面的文章 1. 建一个ReuseWebDriver类 import java.io ...

  10. 查看Laravel版本号的三种方法

    1:最简单的用命令行实现 php artisan --version 2:查看文件 vendor\laravel\framework\src\Illuminate\Foundation\Applica ...