场景

我们在测试一个web 应用时,经常出现翻页的情况,下面介绍翻页场景

代码

#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: 翻页.py
@time: 2018-09-26 11:14
@desc:
'''
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time driver = webdriver.Firefox()
#添加智能等待
driver.implicitly_wait(10) driver.get('https://www.baidu.com/')
driver.find_element_by_id('kw').send_keys('uniquefu') driver.find_element_by_id('su').click() page = driver.find_element_by_id('page')
pages = page.find_elements_by_tag_name('a') #查找所有翻页跳转链接
time.sleep(5) js = 'document.documentElement.scrollTop=10000'
total = 0 #页面数
is_next_page = True #存在下一页
page_num = 0 #要点击的页面号 #往后翻页
while page_num <10: #也可以通过is_next_page进行判断循环
driver.execute_script(js)
page_num = page_num + 1 #设置页号为下一页
total = page_num #记录页面数
value=str(page_num)
try:
#查找指定页面
xpath= "//div[@id='page']/a[contains(@href,'pn=%s')]" %value
print(xpath)
one_page = driver.find_element_by_xpath(xpath)
one_page.click()
time.sleep(1)
driver.execute_script(js)
time.sleep(1) except:
print('no next page')
is_next_page = False
total = total - 1
break #往前翻页
while total >= 0: driver.execute_script(js) try:
total = total -1
value = str(total)
xpath = "//div[@id='page']/a[contains(@href,'pn=%s')]" % value
print(xpath)
one_page = driver.find_element_by_xpath(xpath)
one_page.click()
time.sleep(1)
driver.execute_script(js)
time.sleep(1) except:
print('no pre page')
break; time.sleep(3)
driver.quit()

 

遇到问题:

selenium.common.exceptions.StaleElementReferenceException: Message: u'Element not found in the cache - perhaps the page has changed since it was looked up' ; Stacktrace:

即在cache中找不到元素,可能是在元素被找到之后页面变换了。 这就说明,当前页面发生跳转之后,存在cache中的与这个页面相关的元素也被清空了,因此跳转后需要重新获取下一个页面翻页链接,然后点击。

备注:

对于类型博客类型的翻页不需要那么麻烦,因为翻页后页面链接不会发生变化

Python+Selenium学习--分页处理的更多相关文章

  1. 【python+selenium学习】Python常见错误之:IndentationError: unexpected indent

    初入python+selenium学习之路,总会遇到这样那样的问题.IndentationError: unexpected indent,这个坑我已经踏进数次了,索性记录下来.都知道Python对代 ...

  2. Python+Selenium学习--自动化测试模型

    前言 一个自动化测试框架就是一个集成体系,在这一体系中包含测试功能的函数库.测试数据源.测试对象识别标准,以及种可重用的模块.自动化测试框架在发展的过程中经历了几个阶段,模块驱动测试.数据驱动测试.对 ...

  3. Python+Selenium学习笔记15 - 读取txt和csv文件

    读取txt的内容并用百度查找搜索 1 # coding = utf-8 2 3 from selenium import webdriver 4 import time 5 6 # 打开浏览器 7 d ...

  4. web自动化测试python+selenium学习总结----selenium安装、浏览器驱动下载

    一.安装selenium 命令安装selenium库 :pip  install -U selenium 查看selenium是否安装成功:pip list PS:有时会有异常,安装失败,可以尝试去s ...

  5. Python+Selenium学习--自动生成HTML测试报告

    前言 在脚本运行完成之后,除了在log.txt 文件看到运行日志外,我们更希望能生一张漂亮的测试报告来展示用例执行的结果.        HTMLTestRunner 是Python 标准库的unit ...

  6. Python+Selenium学习--异常截图

    前言 Webdriver 提供错误截图函数get_screenshot_as_file(),可以帮助我们跟踪bug,在脚本无法继续执行时候, get_screenshot_as_file()函数将截取 ...

  7. Python+Selenium学习--cookie处理

    场景 有时候我们需要验证浏览器中是否存在某个cookie,因为基于真实的cookie 的测试是无法通过白盒和集成测试完成的.webdriver 可以读取.添加和删除cookie 信息.webdrive ...

  8. Python+Selenium学习--控制浏览器控制条

    场景 有时候web 页面上的元素并非直接可见的,就算把浏览器最大化,我们依然需要拖动滚动条才能看到想要操作的元素,这个时候就要控制页面滚动条的拖动,但滚动条并非页面上的元素,可以借助JavaScrip ...

  9. Python+Selenium学习--下载文件

    场景 webdriver 允许我们设置默认的文件下载路径.也就是说文件会自动下载并且存在设置的那个目录中,下面以firefox及chrome为例 代码 Firefox下载 为了让Firefox浏览器能 ...

随机推荐

  1. JS 异步分段上传文件

    为了解决大文件上传 (PHP上传最大限制2GB) 同时为了解决文件上传是对服务器造成的压力 可以通过分段上传解决这个问题,这得益于HTML5开发的file API 前台代码: 引用了进度条插件myPr ...

  2. vbox 按照增强工具 centos7

    命令:mount -t auto /dev/cdrom /mnt/cdrom 这命令就是把CentOS CDROM挂载在/mnt/cdrom目录中,这样我们就可以访问光盘里面的内容了.执行“mount ...

  3. TP框架(接口文档模板框架)

    https://blog.csdn.net/s390124971/article/details/51995497

  4. sse 与 socket 摘录-推送常用技术

    推送技术常用的集中实现的实现方式 01.短连接轮询 02.长轮询 03.iframe流: 04.WebSocket 05.Server-sent Events(sse)

  5. python中函数基础

    函数 什么是函数? 函数分为内置函数和自定义函数 定义:在程序中具备某一功能的工具.在使用之前需准备该工具(函数的定义),遇到应用场景拿来就用(后引用). 为什么要用函数? 1.代码冗余 程序组织结构 ...

  6. How to Pronounce the Word OR

    How to Pronounce the Word OR Share Tweet Share Tagged With: OR Reduction Study the OR reduction.  Th ...

  7. Haskell语言学习笔记(77)Data.HashSet

    安装 unordered-containers $ cabal install unordered-containers Installed unordered-containers-0.2.9.0 ...

  8. cap文件的格式说明

    前面24个字节是.cap文件的文件头. 头信息对应的结构体为:struct pcap_file_header {  bpf_u_int32 magic;  u_short version_major; ...

  9. c++之enum的好处与 define 的区别

    转载自 https://blog.csdn.net/zhh464626057/article/details/41038933 什么时候需要用到enum呢?就是变量的数值在几个范围之间.red,blu ...

  10. Signing Your Applications(Android签名相关)

    In this document Signing Overview Signing in Debug Mode Signing in Release Mode Signing Android Wear ...