# -*- 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. 【贪心算法】CF Emergency Evacuation

    题目大意 vjudge链接 给你一个车厢和一些人,这些人都坐在座位上,求这些人全部出去的时间最小值. 样例1输入 5 2 71 11 21 32 32 44 45 2 样例1输出 9 样例2输入 50 ...

  2. lumen-ioc容器测试 (1)

    lumen-ioc容器测试 (1) lumen-ioc容器测试 (2) lumen-ioc容器测试 (3) lumen-ioc容器测试 (4) lumen-ioc容器测试 (5) lumen-ioc容 ...

  3. 第三十三章 linux常规练习题(二)

    一.练习题一 1.删除用户基本组shanghai03.发现无法正常删除,怎样才能将其删除掉,不能删除用户.2.打开多个xshell窗口连接登录同一虚拟机,使用不同的用户登录多次,分别使用w和who命令 ...

  4. 微信小程序项目wx-store代码详解

    这篇文章会很长,非常长,特别长,无敌长. 真的是挤牙膏般的项目进度,差不多是8月底有开始这个项目的想法,时至今日都1个多月了,抛去频繁的加班时间,王者时间,羽毛球时间...见缝插针的写这个项目,我竟然 ...

  5. Linux系统安装MySQL(2020最新最详细)

    2020最新Linux系统发行版ContOS7演示安装MySQL. 为防止操作权限不足,建议切换root用户,当然如果你对Linux命令熟悉,能够自主完成权限更新操作,可以不考虑此推荐. 更多命令学习 ...

  6. poj1050 To the Max(降维dp)

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 49351   Accepted: 26142 Desc ...

  7. abstract关键字的说法

    含有abstract修饰符的class即为抽象类,abstract 类不能创建的实例对象.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不必 ...

  8. Vue (学习第四部 前端项目搭建流程 )

    目录 客户端项目搭建 创建项目目录 初始化项目 安装路由 Vue-router 下载安装路由组件 配置路由 初始化路由对象 注册路由信息 在视图函数中显示路由对应的内容 路由对象提供的操作 页面跳转 ...

  9. 使用TypeScript给Vue 3.0写一个指令实现组件拖拽

    最近在用vue3重构后台的一个功能.一个弹窗组件,弹出一个表单.然后点击提交. 早上运维突然跑过来问我,为啥弹窗挡住了下边的表格的数据,我添加的时候,都没法对照表格来看了.你必须给我解决一下. 我参考 ...

  10. PostgreSQL 报错 Problem running post-install step.Installation may not complete correctlyThe database cluster initialisation failed.

    在点击完next后安装进度条到最后会弹出题目这个错误 之前选择locale选择china/Singapore 或者china/hongkong都会报错 我的解决方案是 不选择,使用默认的就不会报错,并 ...