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. python pip包管理器安装

    下载   http://peak.telecommunity.com/dist/ez_setup.py 执行:python ez_setup.py 下载:  http://pypi.python.or ...

  2. PHP微信公众平台OAuth2.0网页授权,获取用户信息代码类封装demo(二)

    一.这个文件微信授权使用的是OAuth2.0授权的方式.主要有以下简略步骤: 第一步:判断有没有code,有code去第三步,没有code去第二步 第二步:用户同意授权,获取code 第三步:通过co ...

  3. Scut游戏服务器免费开源框架--快速开发(1)

    Scut快速开发(1) 1        开发环境 需要安装的软件 a)        VS2010开发工具(.Net Framework 4.0以上) 2        HelloWorld 2.1 ...

  4. iOS -- SpriteKit框架之SKPhysicsBody的移动和连接

      SKPhysicsBody的受力移动 SKPhysicsBody对象不同于SKSpriteNode对象.SKSpriteNode对象可以通过添加动作来进行移动,SKPhysicsBody对象却没有 ...

  5. openssl之EVP系列之11---EVP_Verify系列函数介绍

    openssl之EVP系列之11---EVP_Verify系列函数介绍     ---依据openssl doc/crypto/EVP_VerifyInit.pod翻译和自己的理解写成     (作者 ...

  6. WinDbg抓取dmp文件

    应用程序发生异常时抓取dmp: adplus.vbs -crash -pn w3wp.exe -y srv*c:\symbols*http://msdl.microsoft.com/download/ ...

  7. 详谈kubernetes滚动更新-1

    系列目录 这个系列分为两个小节,第一个小节介绍deployment滚动更新时,deployment.replicaset.pod的细节以及创建过程以及deployment版本管理的方式 第二个小节将介 ...

  8. The type List is not generic(转载)

    错误:The type List is not generic; it cannot be parameterized with arguments <Activity> 代码如下: pu ...

  9. 2809: [Apio2012]dispatching

    2809: [Apio2012]dispatching Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 3102  Solved: 1641 [Sub ...

  10. 小程序 富文本 的页面展示 json 数据处理 go-echo 为小程序提供feed流服务

    go生成页面 返回给web-view {{define "DBHtmlCode"}} <!DOCTYPE html> <html lang="zh-cm ...