# -*- 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. Nexus 安装教程

    Nexus 安装教程 一. CentOS设置 1. 更换阿里源 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/ ...

  2. document.all.WebBrowser为空或不是对象

    项目中也想用这个功能,发现出错,经过测试,一定要加<object id="WebBrowser" width=0 height=0 classid="CLSID:8 ...

  3. Jmeter入门(6)- 参数化

    一.什么是参数化 为什么要参数化? 在发送大量的请求时,键对值是写死的,每次请求都需要去修改,无法实现快速添加的需求.想要快速实现该需求,就需要用到参数化. 什么是参数化? 根据需求动态获取数据并进行 ...

  4. 蓝桥杯 Island Hopping Java代码

          问题描述 太平洋岛网公司(PLN)已经瞄向了太平洋中的一些群岛.这些群岛没有快捷的互联网连接.PLN计划向群岛提供互联网服务,以开发这个太平洋中潜在的市场.每组群岛的核心岛屿已经被深海电缆 ...

  5. vscode自定义插件安装位置

    vscode的插件默认安装位置在: C:\Users\用户名\.vscode\extensions 如果不想将插件安装在C盘,可以自定义一个目标位置存储,使用如下: 右键快捷方式,在原本的目标后加入- ...

  6. 如何使用 Azure Active Directory 认证和 Microsoft Graph 构建 Blazor Web 应用

    如何使用 Azure Active Directory 认证和 Microsoft Graph 构建 Blazor Web 应用 英文原文:https://developer.microsoft.co ...

  7. mysql 架构简介

    mysql的逻辑架构 第一层:进行连接处理.权限认证.安全校验等. 当客户端(应用)连接到mysql服务器时,服务器会创建使用一个线程进行处理连接(少量的线程服务大量的连接),随后服务器需要对该连接进 ...

  8. 动态规划专题一:线性dp

    第一篇博客随笔,被迫写的bushi 上课讲的动态规划入门,还是得总结一下吧 背包 01背包 背包有容量限制,每一件物品只能够取一件(这就是为什么j从V至v[i]循环的原因) 思路:f数组表示当前状态的 ...

  9. Zotero使用教程

    之前一直想有一个管理文献的好工具,但囿于麻烦都没有去做.最近需要阅读大量的文献,便重新拾起了这个念头,在几经搜索后,选定了Zotero作为文献管理工具. 至于为什么选择这个软件,我也许并说不清,网上有 ...

  10. Linux部署常用命令

    1../startup.sh 开启tomcat服务 2.rm -f file1  删除文件 3.rmdir dir xxx 删除目录 4.Tab按钮 自动补全文件名称功能 5.gzip file1  ...