Python+Selenium学习--分页处理
场景
我们在测试一个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学习--分页处理的更多相关文章
- 【python+selenium学习】Python常见错误之:IndentationError: unexpected indent
初入python+selenium学习之路,总会遇到这样那样的问题.IndentationError: unexpected indent,这个坑我已经踏进数次了,索性记录下来.都知道Python对代 ...
- Python+Selenium学习--自动化测试模型
前言 一个自动化测试框架就是一个集成体系,在这一体系中包含测试功能的函数库.测试数据源.测试对象识别标准,以及种可重用的模块.自动化测试框架在发展的过程中经历了几个阶段,模块驱动测试.数据驱动测试.对 ...
- Python+Selenium学习笔记15 - 读取txt和csv文件
读取txt的内容并用百度查找搜索 1 # coding = utf-8 2 3 from selenium import webdriver 4 import time 5 6 # 打开浏览器 7 d ...
- web自动化测试python+selenium学习总结----selenium安装、浏览器驱动下载
一.安装selenium 命令安装selenium库 :pip install -U selenium 查看selenium是否安装成功:pip list PS:有时会有异常,安装失败,可以尝试去s ...
- Python+Selenium学习--自动生成HTML测试报告
前言 在脚本运行完成之后,除了在log.txt 文件看到运行日志外,我们更希望能生一张漂亮的测试报告来展示用例执行的结果. HTMLTestRunner 是Python 标准库的unit ...
- Python+Selenium学习--异常截图
前言 Webdriver 提供错误截图函数get_screenshot_as_file(),可以帮助我们跟踪bug,在脚本无法继续执行时候, get_screenshot_as_file()函数将截取 ...
- Python+Selenium学习--cookie处理
场景 有时候我们需要验证浏览器中是否存在某个cookie,因为基于真实的cookie 的测试是无法通过白盒和集成测试完成的.webdriver 可以读取.添加和删除cookie 信息.webdrive ...
- Python+Selenium学习--控制浏览器控制条
场景 有时候web 页面上的元素并非直接可见的,就算把浏览器最大化,我们依然需要拖动滚动条才能看到想要操作的元素,这个时候就要控制页面滚动条的拖动,但滚动条并非页面上的元素,可以借助JavaScrip ...
- Python+Selenium学习--下载文件
场景 webdriver 允许我们设置默认的文件下载路径.也就是说文件会自动下载并且存在设置的那个目录中,下面以firefox及chrome为例 代码 Firefox下载 为了让Firefox浏览器能 ...
随机推荐
- python linux 下开发环境搭建
1.1: 在虚拟环境目录下安装 ipython => pip install ipython 1.2: 简单的使用 => ipthyon => print("heollo ...
- leetcode1008
class Solution: def __init__(self): self.root = None def construct(self,order,root,i): if i==len(ord ...
- leetcode315
public class Solution { public List<Integer> countSmaller(int[] nums) { List<Integer> re ...
- 前后台交互实现点击超链接通过指定的 url 去网络或者文件服务器下载文件
前台 VUE 界面: <el-table-column prop="attachment" align="center" label="附件详情 ...
- 【JEECG技术文档】online自定义模板的使用
一. 业务背景 客户需要快速开发一个信息采集的功能模块,并使用已规划好的页面,实现个性化页面展示,使用标准左右布局的Table或DIV风格的页面表现力不强,不能满足客户的个性化页面需要 二. 需求 1 ...
- xampp默认mysql数据库root密码的修改
因为安装xampp后的mysql默认用户root的密码为空,而比如部署Testlink时需要提供数据库密码,此时就需要给root设定密码(网上有些方法,大同小异,但是可能都未标明关键点,未一些出上手的 ...
- Windows 8 禁用强制驱动签名
打开管理员模式的命令行,运行命令: bcdedit.exe -set loadoptions DDISABLE_INTEGRITY_CHECKS bcdedit/set testsigning on ...
- Word Ladder 有必要深究。非图的广度优先遍历。标记
感觉很生疏. https://leetcode.com/problems/word-ladder/
- qurtz.net(转载)
Quartz+TopShelf实现Windows服务作业调度 Quartz:首先我贴出来了两段代码(下方),可以看出,首先会根据配置文件(quartz.config),包装出一个Quartz.Co ...
- C# 图像处理:将图像(24位真彩)转为 8位灰度图像 采用了内存法,大大提高了效率
/// <summary> /// 将源图像灰度化,并转化为8位灰度图像. /// </summary> /// <param name="original&q ...