# -*- 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:\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:\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"C:\Users\eli\AppData\Local\Google\Chrome\User Data\Default"))

这个地方具体的查找方法为:用Chrome地址栏输入chrome://version/,查看自己的“个人资料路径”,然后在浏览器启动时,调用这个配置文件

这里面有个坑,就是获取的配置为:C:\Users\ghhy00010\AppData\Local\Google\Chrome\User Data\Default

很多资料都显示为:C:\Users\ghhy00010\AppData\Local\Google\Chrome\User Data,然后一运行就报错,报的错

UnboundLocalError: local variable 'driver' referenced before assignment,一直提示浏览器没有定义。

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()

练习启动各种浏览器的同时加载插件:Firefox, Chrome, IE的更多相关文章

  1. 原生js开发,无依赖、轻量级的现代浏览器图片懒加载插件,适合在移动端开发使用

    优势 1.原生js开发,不依赖任何框架或库 2.支持将各种宽高不一致的图片,自动剪切成默认图片的宽高 比如说你的默认图片是一张正方形的图片,则各种宽度高度不一样的图片,自动剪切成正方形. 完美解决移动 ...

  2. Selenium2(WebDriver)总结(一)---启动浏览器、设置profile&加载插件

    本文主要记录下在使用selenium2/webdriver时启动各种浏览器的方法.以及如何加载插件.定制浏览器信息(设置profile)等 环境搭建可参考我的另一篇文章:http://www.cnbl ...

  3. Selenium2启动浏览器且加载插件

    一.SELENIUM2启动浏览器 注意: SELENIUM2在启动浏览器时,都是启动一个干净的没有任务 插件及cookies信息的浏览器,即使是你之前的浏览器有设置过代理,到自动化启动时,也是没有代理 ...

  4. windows系统打开火狐浏览器提示“无法加载你的firefox配置文件”

    win7系统自带IE浏览器,还是有部分用户使用不习惯,选择下载第三方浏览器,比如:火狐.谷歌.360浏览器等.最近有Win7系统用户在重新安装火狐浏览器后发现打不开,并提示“无法加载你的firefox ...

  5. 如何调试异步加载的js文件(浏览器调试动态加载js)

    描述 1:jQuery->var obj= new $.js_Obj():等异步加载js文件,执行方法. obj.method(): 2:页面估计不变,通过声明不同的js文件,进行页面内容的转换 ...

  6. Spring Boot 启动(二) Environment 加载

    Spring Boot 启动(二) Environment 加载 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 上一节中 ...

  7. 通过chrome浏览器分析网页加载时间

    今天趁着下班的时间看了下chrome浏览器的网页加载时间分析工具和相关文档,简单写点儿东西记录一下. 以百度首页加载为例,分析下一张图片1.jgp(就是背景图)的加载时间 看右侧的Timing标签,从 ...

  8. IE浏览器中的加载项怎么删除

    IE浏览器中的加载项是一些软件或者浏览器的功能控件,我们可以通过禁用.开启来控制是否使用某些加载项,同时可以将一些加载项删除. 比如当我们遇到了一些不好的加载项,想要将它删除,通过这篇经验,教大家怎么 ...

  9. (4.21)SQL Server数据库启动过程(用户数据库加载过程的疑难杂症)

    转自:指尖流淌 http://www.cnblogs.com/zhijianliutang/p/4100103.html SQL Server数据库启动过程(用户数据库加载过程的疑难杂症) 前言 本篇 ...

随机推荐

  1. C#数据结构-队列

    队列作为线性表的另一个数据结构,只允许在表的前端进行删除操作,而在表的后端进行插入操作,和栈一样,队列是一种操作受限制的线性表. 先来看下用法: Queue queue = new Queue(); ...

  2. Linux操作系统的介绍和安装教程(Centos6.4)

    路漫漫其修远兮,吾将上下而求 Linux的简单介绍 Linux最初是由芬兰赫尔辛基大学学生Linus Torvalds开发的,由于自己不满意教学中使用的MINIX操作系统, 所以在1990年底由于个人 ...

  3. Ubuntu使用mail命令发送邮件

    sudo apt-get install mailutils   如下命令发送邮件:    mail -s "Test mail from ubuntu" ckboss@y< ...

  4. B. Two Arrays 解析(思維)

    Codeforce 1417 B. Two Arrays 解析(思維) 今天我們來看看CF1417B 題目連結 題目 略,請直接看原題. 前言 a @copyright petjelinux 版權所有 ...

  5. oracle 查询数据库锁及锁处理

    1.数据库锁表查询语句: SELECT SESS.SID, SESS.SERIAL#, LO.ORACLE_USERNAME, LO.OS_USER_NAME, AO.OBJECT_NAME 被锁对象 ...

  6. equals()方法和hashCode()方法详解

    equals()方法和hashCode()方法详解 1. Object类中equals()方法源代码如下所示: /** * Object类中的equals()方法 */ public boolean ...

  7. Flask简介与启动服务器

    Flask 一.简介 官方文档:http://flask.pocoo.org/ http://www.pythondoc.com/flask/index.html(中文) 1.概述 flask是一个非 ...

  8. think PHP5.1使用时 session重定向丢失问题

    查了很多资料,也看了redirect底层代码,具体来说,还是多个用的地方不太对.做个笔记防忘记: 遇重定向后丢失session时: 1.php.ini配置文件,不要自动启动,默认是0,session. ...

  9. 第 2 篇:上手 Vue 展示 todo 列表

    作者:HelloGitHub-追梦人物 追梦人物的 Vue 系列教程在他的博客已经全部更新完成,地址: https://www.zmrenwu.com/courses/vue2x-todo-tutor ...

  10. Java_静态代理与Lambda

    静态代理 要点: 公共接口 真实角色 代理角色 public class StaticProxy { public static void main(String[] args) { You you ...