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

最新文章

  1. 运用Mono.Cecil 反射读取.NET程序集元数据
  2. asp.net mvc 中 一种简单的 URL 重写
  3. javascript 方法实例
  4. 手把手教你搭建LyncServer2013之部署及配置监控功能(十八)
  5. Perl Print Win32 Console Windows 控制台 print Unicode 问题
  6. 关于Java函数传参以及参数在函数内部改变的问题——JAVA值传递与引用最浅显的说明!
  7. Sold out
  8. Sql Server 时间格式
  9. 数据库分库分表(sharding)系列(五) 一种支持自由规划无须数据迁移和修改路由代码的Sharding扩容方案
  10. Hadoop学习之YARN框架
  11. ZLG_GUI和3D显示的移植
  12. ios 苹果手机硬件摘要
  13. 11、Libgdx的音频
  14. babel在项目里的使用
  15. webpack学习笔记--区分环境
  16. Xamarin Essentials教程地理定位Geolocation
  17. Java笔记一JAVA安装环境变量配置
  18. <转> plsql dev中Dynamic Performance Tables not accessible分析解决
  19. !important的用法
  20. [重要] Django 多条件多表查询实例问题

热门文章

  1. RESP协议
  2. rsync安装使用中出现的报错
  3. go 结构体与方法
  4. 【荐】JavaScript图片放大技术(放大镜)示例代码
  5. C# 将Excel里面的数据填充到DataSet中
  6. 线程池CachedThreadPool
  7. 想用Nginx代理一切?行!
  8. 删除指定路径下指定天数之前(以文件的创建日期为准)的文件:BAT + REG + Ritchie Lawrence 日期函数
  9. windows搭建ftp
  10. django JsonResponse返回中文时显示unicode编码(\u67e5\u8be2)