【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="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. }
时间: 2024-08-24 23:35:04

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

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

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

selenium跳过https的问题

背景: 周六产品给我反馈:支付成功页面后会提示这个,问自动化为什么没有发现这样的问题 第一反应:这个地址肯定被举报了,我也肯定没有设置过安全链接,因为都没有见过这样的网址,如果有问题,应该会直接出错,先本地重现 1.先确认了发生的时间,是在昨天就发生了,而我的定时任务是在晚上2点跑,正常是可以发现这样的问题,脚本的场景是:一旦支付成功,需要获取页面上的内容,一旦跳出这样的页面,文本获取不到,用例就不在执行,我有试过用脚本跑了一遍 确认没有发生这样的问题, 2.我有手动的去操作重显场景,还是没有发

python + selenium 跳坑之旅 (第一弹)

哪怕你再精通selenium,陪伴测试人员最多的还是定位问题. 第一个坑:     要定位的页面,出现html嵌套,也就是ifrmae.在html代码页面很直观的就能发现这个东西.实际就是在一个页面中引用了一个子页面.这样造成我们再用xpath或者css定位的时候直接找不到元素.这个时候我们需要用到一个新的东西来为她定位,其思路大体为:第一步,从父页面切换至子页面:第二步,在子页面定位元素:第三步,返回父页面(这个看个人需要,具体留在哪里需要结合实际). 第一步的实现需要用到一个方法:switc

Selenium入门7 跳入/跳出frame

如果网页内嵌iframe,那么iframe里的元素是无法直接定位的,需要使用switch_to_frame进入frame操作: 之后需要再操作页面上非嵌入在iframe里的元素,需要使用switch_to_default_content跳回初始页面. 首先在脚本的文件夹里新建一个test3.html文件,将以下内容拷贝进去保存,作为测试用的页面.保存好了用浏览器打开看一下.也可找网上嵌入iframe的页面.自己写可以节约找网页的时间. <!DOCTYPE html> <html lang

Selenium—通过cookies跳过验证码登录

通过cookies跳过验证码登录 现在很多的登录都需要验证,而验证相对复杂,需要花费大量的时间,那么我们就可以通过cookie,来跳过登录 cookie怎么获取 我们可以通过手动登录,来获取登录时的cookie值 通过Fiddler抓包获取cookie值 通过开发者工具--Network 中获取cookie值 以 https://www.gglott.com.cn/WXDefault.aspx?home=1 登录为例 添加cookie driver.add_cookie() 思路: 手动登录获取

使用selenium+python;在页面A点击一个按钮跳转到页面B,页面A没有关闭,浏览器打开了一个B,切换窗口定位页面B的元素

转自:http://blog.csdn.net/hhabc123456789/article/details/21862139 # -*- coding:cp936 -*-__author__ = 'Administrator' import unittest,time,refrom selenium import webdriver class Untitled(unittest.TestCase):    def setUp(self):        self.driver = webdr

selenium如何跳转到iframe

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

Python+selenium之跳过测试和预期失败

在运行测试时,需要直接跳过某些测试用例,或者当用例符合某个条件时跳过测试,又或者直接将测试用例设置为失败.unittest单元测试框架提供了实现这些需求的装饰器. 1.unittest.skip(reason) 无条件地跳过装饰的测试,说明跳过测试的原因 2.unittest.skipIf(condition,reason) 如果条件为真时,跳过装饰的测试. 3.unittest.skipUless(condition,reason) 跳过装饰的测试,除非条件为真 4.unittest.expe

【Python + Selenium】之unittest测试用例满足条件,进行跳过测试Skip

直接上代码: __author__ = 'zc' import unittest class demoSkipTest(unittest.TestCase): a = 70 b = 50 print(a%b) def test_add(self): """加法""" result = self.a + self.b self.assertEqual(result, 40) def test_add_2(self): self.skipTest(&