webdriver高级应用- 浏览器中新开标签页(Tab)
#encoding=utf-8
import unittest
from selenium import webdriver
import time
import win32api, win32con VK_CODE ={'ctrl':0x11, 't':0x54, 'tab':0x09} # 键盘键按下
def keyDown(keyName):
win32api.keybd_event(VK_CODE[keyName], 0, 0, 0)
# 键盘键抬起
def keyUp(keyName):
win32api.keybd_event(VK_CODE[keyName], 0, win32con.KEYEVENTF_KEYUP, 0) # 封装的按键方法
def simulateKey(firstKey, secondKey):
keyDown(firstKey)
keyDown(secondKey)
keyUp(secondKey)
keyUp(firstKey) class TestDemo(unittest.TestCase):
def setUp(self):
# 获取浏览器驱动实例
self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
#self.driver = webdriver.Firefox(executable_path = "e:\\geckodriver")
def test_newTab(self):
# 等待3秒,等待浏览器启动完成
time.sleep(3)
# 使用for循环,再新开两个新的标签页
for i in range(2):
simulateKey("ctrl", "t")
# 通过Ctrl + tab组合键,将当前页面切换为默认页面,
# 也就是最先打开的标签页
simulateKey("ctrl", "tab")
# 访问搜狗首页
self.driver.get("http://sogou.com")
self.driver.find_element_by_id("query").send_keys(u"光荣之路")
self.driver.find_element_by_id("stb").click()
time.sleep(3)
#self.assertTrue(u"乔什•卢卡斯" in self.driver.page_source) # 获取所有的打开的窗口句柄
all_handles = self.driver.window_handles
print len(all_handles)
for handle in all_handles:
# 将当前窗口句柄切换至第二个标签页
self.driver.switch_to.window(handle)
print self.driver.title
if u"输入法" not in self.driver.page_source:
self.driver.get("http://www.baidu.com")
self.driver.find_element_by_id("kw").send_keys(u"WebDriver实战宝典")
self.driver.find_element_by_id("su").click()
time.sleep(3)
self.assertTrue(u"实战宝典" in self.driver.page_source)
elif (u"输入法" not in self.driver.page_source) and ("WebDriver" not in self.driver.page_source):
# 将当前窗口的句柄切换至第三个标签页
self.driver.get("http://www.iciba.com")
time.sleep(3)
self.assertTrue(u"查词" in self.driver.page_source) def tearDown(self):
# 退出浏览器
self.driver.quit() if __name__ == '__main__':
unittest.main()
基于火狐:
#encoding=utf-8
import unittest
from selenium import webdriver
import time
import win32api, win32con VK_CODE ={'ctrl':0x11, 't':0x54, 'tab':0x09} # 键盘键按下
def keyDown(keyName):
win32api.keybd_event(VK_CODE[keyName], 0, 0, 0)
# 键盘键抬起
def keyUp(keyName):
win32api.keybd_event(VK_CODE[keyName], 0, win32con.KEYEVENTF_KEYUP, 0) # 封装的按键方法
def simulateKey(firstKey, secondKey):
keyDown(firstKey)
keyDown(secondKey)
keyUp(secondKey)
keyUp(firstKey) class TestDemo(unittest.TestCase):
def setUp(self):
# 获取浏览器驱动实例
#self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
self.driver = webdriver.Firefox(executable_path = "e:\\geckodriver")
def test_newTab(self):
# 等待3秒,等待浏览器启动完成
time.sleep(3)
# 使用for循环,再新开两个新的标签页
for i in range(2):
simulateKey("ctrl", "t")
# 通过Ctrl + tab组合键,将当前页面切换为默认页面,
# 也就是最先打开的标签页
simulateKey("ctrl", "tab")
# 访问搜狗首页
self.driver.get("http://sogou.com")
self.driver.find_element_by_id("query").send_keys(u"光荣之路")
self.driver.find_element_by_id("stb").click()
time.sleep(3)
#self.assertTrue(u"乔什•卢卡斯" in self.driver.page_source) # 获取所有的打开的窗口句柄
all_handles = self.driver.window_handles
print len(all_handles)
# 将当前窗口句柄切换至第二个标签页
self.driver.switch_to.window(all_handles[1])
self.driver.get("http://www.baidu.com")
self.driver.find_element_by_id("kw").send_keys(u"WebDriver实战宝典")
self.driver.find_element_by_id("su").click()
time.sleep(3)
self.assertTrue(u"吴晓华" in self.driver.page_source) # 将当前窗口的句柄切换至第三个标签页
self.driver.switch_to.window(all_handles[2])
self.driver.get("http://www.baidu.com")
self.driver.find_element_by_id("kw").send_keys("selenium")
self.driver.find_element_by_id("su").click()
time.sleep(3)
self.assertTrue("www.seleniumhq.org" in self.driver.page_source) def tearDown(self):
# 退出浏览器
self.driver.quit() if __name__ == '__main__':
unittest.main()
webdriver高级应用- 浏览器中新开标签页(Tab)的更多相关文章
- window.open新打开窗口与新开标签页
最近在使用window.open时忽略了一个细节问题:window.open新打开一个窗口,但是有时却是新打开一个窗口有时打开一个新标签页.虽然对一般的需求来说,这个两种情况都无所谓,但是对于那种有强 ...
- 利用localStorage实现浏览器中多个标签页之间的通信
原理: localStorage是浏览器存储数据的容器,而且它是多页面共享的,利用localStorage多页面共享的特性,可以实现多个标签页的通信. 比如: 一个标签页发送消息(将发送的消息设置到l ...
- 利用cookie实现浏览器中多个标签页之间的通信
原理: cookie是浏览器端的存储容器,而且它是多页面共享的,利用cookie多页面共享的特性,可以实现多个标签页的通信. 比如: 一个标签页发送消息(将发送的消息设置到cookie中),一个标签页 ...
- google浏览器打开新的标签页显示http://www.google.com.hk/url?sa=p&hl=zh-CN&……
chrome的版本:51.0.2704.106 m使用该版本的chrome后,每次打开新标签页,都会提示“无法访问此网站”.并自动跳转到一个地址“http://www.google.com.hk/ur ...
- 利用webSocket实现浏览器中多个标签页之间的通信
webSoket用来实现双向通信,客户端和服务端实时通信. webSoket优点和缺点? 优点:对于前端来说,使用简单,功能灵活,如果部署了webSocket服务器,可以实现实时通信. 缺点:需要服务 ...
- Python+Selenium练习篇之15-在浏览器中新开一个tab
本文介绍如selenium方法打开一个新的tab,我们知道在浏览器里,我们按住 ctrl+ t 就可以新打开一个tab.所以我们学习如何利用webdriver中send_key 的方法去触发ctrl+ ...
- chrome打开收藏夹的网站在新的标签页
chrome浏览器在新的标签页打开收藏夹的网址,现在设置不了,而且右键,在新标签页中打开有点烦..下面说说直接打开的方式. 方法1: 鼠标滚轮,直接点击收藏夹的网址,即可 方法2: ctrl + 鼠标 ...
- Google 浏览器设置打开超链接到新窗口标签页
一.windows 按住Ctrl + 鼠标点击,在新窗口打开,停留在当前页面: 按住Ctrl + Shift + 鼠标点击,在新窗口打开,停留在新窗口: 登录Google账号,管理Google账号, ...
- Google浏览器设置搜索打开新的标签页
每次用google搜索的时候,点击进去原来的搜索页面就不见了 ,其实可以设置打开新的标签页 1.登陆Chrome 2.登陆这个网址https://www.google.com/preferences ...
随机推荐
- python的subprocess模块(写的不错留作查询)
python的subprocess模块 subprocess模块是python从2.4版本开始引入的模块.主要用来取代 一些旧的模块方法,如os.system.os.spawn*.os.popen*. ...
- <Android 应用 之路> 天气预报(一)
Android天气预报客户端 设计思路 欢迎界面,版本号,应用名 + 数据后台加载(所有城市的信息获取) 数据加载完成后跳转到显示界面,显示所有查询到的城市的天气信息 欢迎界面和天气显示界面分别为单独 ...
- gitlab api批量操作 批量添加用户
import os,time import requests,json # def downloadFile(name, url): # headers = {'Proxy-Connection': ...
- redmine安装详解
1.Linux:centos6.4(32位)2.Gcc的编译环境.使用make命令编辑.yum install gcc-c++ 3.PCRE PCRE(Perl Compatible Regular ...
- GPnP profile内容
<?xml version="1.0" encoding="UTF-8"?> <gpnp:GPnP-Profile Version=&quo ...
- Leetcode重点 250题-前400 题
删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...
- DaemonSet 典型应用场景【转】
Deployment 部署的副本 Pod 会分布在各个 Node 上,每个 Node 都可能运行好几个副本.DaemonSet 的不同之处在于:每个 Node 上最多只能运行一个副本. DaemonS ...
- Python re module (regular expressions)
regular expressions (RE) 简介 re模块是python中处理正在表达式的一个模块 r"""Support for regular expressi ...
- mysql 定时任务job
mysql 定时任务job 1.通过show EVENTS显示当前定义的事件 2.检查event_scheduler状态:SHOW VARIABLES LIKE 'event_scheduler' 3 ...
- Mysql常用运算符与函数汇总
Mysql常用运算符与函数汇总 本文给大家汇总介绍了mysql中的常用的运算符以及常用函数的用法及示例,非常的全面,有需要的小伙伴可以参考下 我们先把数据表建好 use test;create tab ...