Python启动浏览器Firefox\Chrome\IE
- # -*- coding:utf-8 -*-
- import os
- import selenium
- from selenium import webdriver
- from selenium.webdriver.common.keys import Keys
- """
- 练习启动各种浏览器:Firefox, Chrome, IE
- 练习启动各种浏览器的同时加载插件:Firefox, Chrome, IE
- """
- def startFirefox():
- """启动安装在默认位置的Firefox浏览器,并自动转到 百度 首页"""
- driver = webdriver.Firefox()
- driver.get("http://www.baidu.com")
- assert("百度" in driver.title)
- elem = driver.find_element_by_name("wd")
- elem.send_keys("selenium")
- elem.send_keys(Keys.RETURN)
- assert "百度" in driver.title
- driver.close()
- driver.quit()
- driver = None
- def startFirefoxWithSpecificLocation():
- """启动安装在 非 默认位置的Firefox浏览器,并自动转到 百度 首页"""
- firefoxBin = os.path.abspath(r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
- os.environ["webdriver.firefox.bin"] = firefoxBin
- driver = webdriver.Firefox()
- driver.get("http://www.baidu.com")
- assert("百度" in driver.title)
- elem = driver.find_element_by_name("wd")
- elem.send_keys("selenium")
- elem.send_keys(Keys.RETURN)
- assert "百度" in driver.title
- driver.close()
- driver.quit()
- driver = None
- def startChrome():
- """启动Chrome浏览器,并自动转到 百度 首页
- 启动Chrome浏览器需要指定驱动的位置
- """
- chrome_driver = os.path.abspath(r"D:\云盘\360云\360云盘\我的自动双向同步文件夹\01-PersonalInfo\DataGuru\12-软件自动化测试Selenium2\1-课程\练习代码_Python版本\Selenium_python\Files\chromedriver.exe")
- os.environ["webdriver.chrome.driver"] = chrome_driver
- driver = webdriver.Chrome(chrome_driver)
- driver.get("http://www.baidu.com")
- assert("百度" in driver.title)
- elem = driver.find_element_by_name("wd")
- elem.send_keys("selenium")
- elem.send_keys(Keys.RETURN)
- assert "百度" in driver.title
- driver.close()
- driver.quit()
- driver = None
- def startIE():
- """启动IE浏览器,并自动转到 百度 首页
- 启动 IE 浏览器需要指定驱动的位置
- """
- ie_driver = os.path.abspath(r"D:\云盘\360云\360云盘\我的自动双向同步文件夹\01-PersonalInfo\DataGuru\12-软件自动化测试Selenium2\1-课程\练习代码_Python版本\Selenium_python\Files\IEDriverServer.exe")
- os.environ["webdriver.ie.driver"] = ie_driver
- driver = webdriver.Ie(ie_driver)
- driver.get("http://www.python.org")
- assert("Python" in driver.title)
- elem = driver.find_element_by_id("id-search-field")
- elem.send_keys("selenium")
- '''
- elem.send_keys(Keys.RETURN)
- assert "百度" in driver.title
- driver.close()
- driver.quit()
- driver = None
- '''
- def start_firefox_with_firebug_plug():
- """启动Firefox,并自动加载插件Firebug"""
- firefoxBin = os.path.abspath(r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
- os.environ["webdriver.firefox.bin"] = firefoxBin
- firefoxProfile = webdriver.FirefoxProfile()
- tempDir = os.getcwd()
- tempDir = os.path.split(tempDir)[0]
- firebugPlugFile = os.path.join(os.path.join(tempDir,"Files"), "firebug-2.0.7.xpi")
- firefoxProfile.add_extension(firebugPlugFile)
- firefoxProfile.set_preference("extensions.firebug.currentVersion", "2.0.7")
- driver = webdriver.Firefox(firefox_profile=firefoxProfile)
- driver.get("http://www.baidu.com")
- def start_chrome_with_chrometomobile_plug():
- """启动Chrome,并自动加载插件Chrome to Mobile"""
- tempDir = os.getcwd()
- tempDir = os.path.split(tempDir)[0]
- chrome_driver_file = os.path.join(os.path.join(tempDir,"Files"), "chromedriver.exe")
- os.environ["webdriver.chrome.driver"] = chrome_driver_file
- chrome_to_mobile_plug_file = os.path.join(os.path.join(tempDir,"Files"), "Chrome-to-Mobile_v3.3.crx")
- chrome_options = webdriver.ChromeOptions()
- chrome_options.add_extension(chrome_to_mobile_plug_file)
- driver = webdriver.Chrome(executable_path=chrome_driver_file,
- chrome_options=chrome_options)
- driver.get("http://www.baidu.com")
- '''
- driver.close()
- driver.quit()
- driver = None
- '''
- def start_firefox_with_default_settings():
- """启动Firefox浏览器, 使用本地配置文件中的选项配置浏览器
- 自动将页面载入过程导出为Har文件,并存放在
- 配置项 extensions.firebug.netexport.defaultLogDir指定的D:\temp\selenium2目录下
- """
- firefox_bin = os.path.abspath(r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
- os.environ["webdriver.firefox.bin"] = firefox_bin
- # 使用从别的机器上拷贝来的浏览器配置
- firefox_profile = webdriver.FirefoxProfile(os.path.abspath(r"D:\Temp\selenium2\Profiles\mm9zxom8.default"))
- # 使用本地的默认配置
- #firefox_profile = webdriver.FirefoxProfile(r"C:\Users\eli\AppData\Roaming\Mozilla\Firefox\Profiles\mm9zxom8.default")
- driver = webdriver.Firefox(firefox_profile=firefox_profile)
- driver.get("http://www.baidu.com")
- driver.get("http://www.baidu.com")
- '''
- driver.close()
- driver.quit()
- driver = None
- '''
- def start_chrome_with_default_settings():
- """启动Firefox浏览器, 使用本地配置文件中的选项配置浏览器"""
- tempDir = os.getcwd()
- tempDir = os.path.split(tempDir)[0]
- chrome_driver = chrome_driver_file = os.path.join(os.path.join(tempDir,"Files"), "chromedriver.exe")
- os.environ["webdriver.chrome.driver"] = chrome_driver
- chrome_options = webdriver.ChromeOptions()
- chrome_options.add_argument("--test-type")
- #chrome_options.add_argument("user-data-dir="+os.path.abspath(r"D:\Temp\selenium2\User Data"))
- chrome_options.add_argument("user-data-dir="+os.path.abspath(r"C:\Users\eli\AppData\Local\Google\Chrome\User Data"))
- driver = webdriver.Chrome(executable_path=chrome_driver,
- chrome_options=chrome_options)
- driver.get("http://www.baidu.com")
- if __name__ == "__main__":
- # 2.启动浏览器时自动加载插件, 如Firefox -> Firebug ; Chrome -> Chrome to Mobile
- # start_firefox_with_firebug_plug()
- # start_chrome_with_chrometomobile_plug()
- # start_firefox_with_default_settings()
- start_chrome_with_default_settings()
- # 1.启动各种浏览器
- #startFirefox()
- #startFirefoxWithSpecificLocation()
- #startChrome()
- #startIE()
Python启动浏览器Firefox\Chrome\IE的更多相关文章
- selenium webdriver 启动三大浏览器Firefox,Chrome,IE
selenium webdriver 启动三大浏览器Firefox,Chrome,IE 1.安装selenium 在联网的情况下,在Windows命令行(cmd)输入pip install selen ...
- selenium+python启动浏览器出错,安装浏览器驱动
WebDriver 支持 Firefox (FirefoxDriver).IE (InternetExplorerDriver).Opera (OperaDriver) 和 Chrome (Chrom ...
- 【Selenium】各浏览器(firefox,chrome,ie)驱动下载地址汇总
前两天使用Selenium分布式时,总抛出异常.更新成最新驱动可以解决.其中chrome异常如下, "platform": "WINDOWS" File &qu ...
- 用Python+selenium打开IE浏览器和Chrome浏览器的问题
这几天在学Python+selenium自动化,对三大浏览器Firefox,Chrome和IE都做了尝试,也都分别下载了对应的webdriver,如:geckodriver.chromedriver. ...
- selenium + firefox/chrome/phantomjs登陆之模拟点击
登陆之模拟点击 工具:python/java + selenium + firefox/chrome/phantomjs (1)windows开发环境搭建 默认已经安装好了firefox 安装pip ...
- Selenium2学习-005-WebUI自动化实战实例-003-三种浏览器(Chrome、Firefox、IE)启动脚本源代码
此文主要通过 三种浏览器(Chrome.Firefox.IE)启动脚本 功能,进行 Selenium2 三种浏览器启动方法的实战实例讲解.文中所附源代码于 2015-01-18 20:33 亲测通过, ...
- python脚本中selenium启动浏览器报错os.path.basename(self.path), self.start_error_message) selenium.common.excep
在python脚本中,使用selenium启动浏览器报错,原因是未安装浏览器驱动,报错内容如下: # -*- coding:utf-8 -*-from selenium import webdrive ...
- 各种浏览器(IE,Firefox,Chrome,Opera)COOKIE修改方法[转]
各种浏览器(IE,Firefox,Chrome,Opera)COOKIE修改方法[转] 网站通过 Cookie 保存了我们访问网站的信息,在不同的浏览器中修改 Cookie 可以如下操作: Firef ...
- IE/Firefox/Chrome等浏览器保存Cookie的位置
IE/Firefox/Chrome等浏览器保存Cookie的位置 原文 http://smilejay.com/2013/04/browser-cookie-location/ 前面写了篇长文( ...
随机推荐
- 学不好Python?我们分析看看正确的学习方法是什么-马哥教育
提起对Python的印象,除了全能之外恐怕就是简单易学了.很多人都在推荐新手学Python入门,毕竟语法简单.语句简洁,所谓“人生苦短我用Python”绝不是一句空话.不过也不能忽视一点:Python ...
- Django - 一对多跨表操作
1.views.py 2.host.html 运行结果: 通过外键,来进行多表取值(多表取值包括后台取值及前端获取): 多表获取数据时,可以通过以下几种方式实现: 1.从query set 中获取某 ...
- JavaScript:一句代码输出重复字符串(字符串乘法)
看到一个题目要求写一个函数times,输出str重复num次的字符串. 比如str:bac num:3 输出:abcabcabc 除了利用循环还有几种方法,我学习研究之后记下以下三种方法. 1 ...
- c/c++排坑(2) -- c语言中的符号重载
所谓的符号重载就是在不同的上下文环境里有不同的意义.甚至有些关键字也被重载而具有好几种意义,这也是C语言的作用域规则对程序员不那么清晰的主要原因. 本章内容摘自<c专家编程>P37. 大家 ...
- 746. Min Cost Climbing Stairs(动态规划)
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- max_element()与min_element()
#include<iostream>#include<algorithm>using namespace std;bool cmp(int i,int j){ return i ...
- Vue项目搭建及原理四
四.Vue-cli工作原理及Vue实例创建,工作原理 (一)Vue-cli原理 1.webpack其实使用了node.js的express网页服务器来进行处理网页相关的数据,相当于使用一个类似apac ...
- Django——5 自定义过滤器及标签
Django 自定义过滤器 自定义标签 简单标签 包含标签 自定义过滤器 自定义过滤器就是一个带有一个或两个参数的Python 函数: - (输入的)变量的值 —— 不一定是字符串形式. - 参数的值 ...
- nyoj 306 二分+dfs
#include<stdio.h> #include<string.h> #define N 200 int Min(int a,int b) { return a>b? ...
- mysql :=和=的区别
:=和=的区别 = 只有在set和update时才是和:=一样,赋值的作用,其它都是等于的作用.鉴于此,用变量实现行号时,必须用:= := 不只在set和update时时赋值的作用,在select也是 ...