selenium之 浏览器导航栏的三个按钮(back、forward、refresh)

来源:互联网 发布:mysql分割字符 编辑:程序博客网 时间:2024/06/05 06:13

更多关于python selenium的文章,请关注我的专栏:Python Selenium自动化测试详解


今天这几个方法非常简单,就是我们能看到的浏览器导航栏的三个按钮:后退、前进、刷新

导航栏按钮

driver.back()
driver.forward()
driver.refresh()

不多说,上代码试试

# -*- coding: utf-8 -*-from selenium import webdriverfrom time import sleepdriver = webdriver.Firefox()driver.maximize_window()driver.get('http://www.baidu.com')print 'base_url: ', driver.current_urldriver.find_element_by_id('kw').send_keys(u'selenium 灰蓝')driver.find_element_by_id('su').click()sleep(2)print 'after search: ', driver.current_urldriver.back()  # backprint 'back to: ', driver.current_urldriver.forward()  # forwardprint 'forward to: ', driver.current_urlsleep(2)driver.refresh()  # refreshprint 'refresh: ', driver.current_urlsleep(2)driver.quit()

结果:
明显能够看到浏览器的后退、前进与刷新的动作。

https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=selenium%20%E7%81%B0%E8%93%9D&rsv_pq=f88ee279000319b4&rsv_t=3537ECuJsvn8biOOxAemTJhRgJA9fRB%2FgYyaBy3sdcQagRhU5jOqEDICoQI&rqlang=cn&rsv_enter=0&rsv_sug3=11&inputT=266&rsv_sug4=266https://www.baidu.com/https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=selenium%20%E7%81%B0%E8%93%9D&rsv_pq=f88ee279000319b4&rsv_t=3537ECuJsvn8biOOxAemTJhRgJA9fRB%2FgYyaBy3sdcQagRhU5jOqEDICoQI&rqlang=cn&rsv_enter=0&rsv_sug3=11&inputT=266&rsv_sug4=266https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=selenium%20%E7%81%B0%E8%93%9D&rsv_pq=f88ee279000319b4&rsv_t=3537ECuJsvn8biOOxAemTJhRgJA9fRB%2FgYyaBy3sdcQagRhU5jOqEDICoQI&rqlang=cn&rsv_enter=0&rsv_sug3=11&inputT=266&rsv_sug4=266

那么什么时候会用呢?

一种情况就是,当你从一个父页面跳转到子页面进行操作,操作完之后没有“返回”之类的按钮或链接,重新进入父页面又很麻烦,back()可以帮你。forward()与此类似,相对没有back()那么常用。

当你修改了页面信息但是没有即时刷新时,可以手动refresh()。

不过需要注意:

当你前进并退回原来的页面或刷新页面之后,页面的元素id是改变了的。不要妄图用原来定位好的WebElement去操作现在的页面元素!否则会出现如下报错:
selenium.common.exceptions.StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up

代码示例下:

# -*- coding: utf-8 -*-from selenium import webdriverdriver = webdriver.Firefox()driver.maximize_window()driver.get('http://www.baidu.com')kw = driver.find_element_by_id('kw')print kw  # 原页面元素IDdriver.refresh()print driver.find_element_by_id('kw')  # 刷新之后的kw元素IDkw.send_keys('csdn')  # 再用原来的元素ID去操作就会抛出异常driver.quit()

来看看输出结果:

<selenium.webdriver.remote.webelement.WebElement (session="19a16be7-f885-4454-aa44-065b35d8377f", element="{307bfd7c-bcb8-4bed-a0f0-164924e9f648}")><selenium.webdriver.remote.webelement.WebElement (session="19a16be7-f885-4454-aa44-065b35d8377f", element="{8eac4fb5-4c8f-4141-b9d5-2ced9b412388}")>Traceback (most recent call last):  File "D:/Code/py files/zhigou/zhigou_framework/test/UI_test/case/testnavigation.py", line 16, in <module>    kw.send_keys('csdn')  # 再用原来的元素ID去操作就会抛出异常  File "C:\APP\Python2.7.10\lib\site-packages\selenium\webdriver\remote\webelement.py", line 334, in send_keys    self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})  File "C:\APP\Python2.7.10\lib\site-packages\selenium\webdriver\remote\webelement.py", line 469, in _execute    return self._parent.execute(command, params)  File "C:\APP\Python2.7.10\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute    self.error_handler.check_response(response)  File "C:\APP\Python2.7.10\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response    raise exception_class(message, screen, stacktrace)selenium.common.exceptions.StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked upStacktrace:    at fxdriver.cache.getElementAt (resource://fxdriver/modules/web-element-cache.js:9407)    at Utils.getElementAt (file:///c:/users/zx/appdata/local/temp/tmpsorvob/extensions/fxdriver@googlecode.com/components/command-processor.js:8992)    at fxdriver.preconditions.visible (file:///c:/users/zx/appdata/local/temp/tmpsorvob/extensions/fxdriver@googlecode.com/components/command-processor.js:10043)    at DelayedCommand.prototype.checkPreconditions_ (file:///c:/users/zx/appdata/local/temp/tmpsorvob/extensions/fxdriver@googlecode.com/components/command-processor.js:12597)    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/zx/appdata/local/temp/tmpsorvob/extensions/fxdriver@googlecode.com/components/command-processor.js:12614)    at DelayedCommand.prototype.executeInternal_ (file:///c:/users/zx/appdata/local/temp/tmpsorvob/extensions/fxdriver@googlecode.com/components/command-processor.js:12619)    at DelayedCommand.prototype.execute/< (file:///c:/users/zx/appdata/local/temp/tmpsorvob/extensions/fxdriver@googlecode.com/components/command-processor.js:12561)

能看到两次输出的WebElement是完全不同的,还用原来的元素去操作当然是不行的:

两次WebElement不同

所以在有可能有页面刷新的时候,不要用这种获取到元素,然后一直操作这个元素的写法;要在刷新之后重新获取一下元素进行操作。

最后插个小广告,如果你想自学,你可以看一些博客和书籍;如果你希望在学习过程中有更多的交流,你可以加入QQ群(219873826)跟一些学习selenium的朋友交流学习,共同进步;如果你对自己的自学能力和自控能力没有信心,我也可以推荐你一些千元价位的python+selenium课程(如龙腾测试

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 大学开学团员证丢了怎么办 研究生开学没有团员证怎么办 研究生开学已经不是团员了怎么办 毕业了要搬宿舍怎么办 中专学历认证已停止怎么办 中专不做学历认证考试怎么办 大学生欠学费被扣毕业证怎么办 考警校体检没过怎么办 美国签证申请预约名字写错怎么办 当兵不从学校走怎么办 门牙崩了一小块怎么办 遇到很难过的事情怎么办 小孩子上课精力不集中怎么办 每天工作都很累压力大怎么办 重体力活搬不动怎么办 大学没参加体测怎么办 英文写的很丑怎么办 患有勃起障碍应该怎么办较好 运动过度小腿肌肉酸痛怎么办 高考有纹身是字怎么办 新生儿测听力没过关怎么办 色弱高考体检时没查出来怎么办 公司福利体检查二对半怎么办 高考体检表复印件丢了怎么办 高考体检表身高填错了怎么办 大学档案高考体检表丢了怎么办 工厂组织体检我有乙肝怎么办 我有乙肝单位组织体检怎么办? 矮腰袜子老掉怎么办 短腰袜子老下滑怎么办 中考体检结果丢了怎么办 咳嗽左胸围一处刺痛怎么办? 阴茎小父母催婚怎么办 头发扎进指甲缝怎么办 指甲缝扎流血了怎么办 中考考差了高中怎么办 骨折后我抽烟了怎么办 五年级科学考不好怎么办 考试连续考差了怎么办 客户说没考虑好怎么办 小孩生殖器痒经常用手抓怎么办