1. # -*- coding:utf-8 -*-
  2. import os
  3. import selenium
  4. from selenium import webdriver
  5. from selenium.webdriver.common.keys import Keys
  6. """
  7. 练习启动各种浏览器:Firefox, Chrome, IE
  8. 练习启动各种浏览器的同时加载插件:Firefox, Chrome, IE
  9. """
  10. def startFirefox():
  11. """启动安装在默认位置的Firefox浏览器,并自动转到 百度 首页"""
  12. driver = webdriver.Firefox()
  13. driver.get("http://www.baidu.com")
  14. assert("百度" in driver.title)
  15. elem = driver.find_element_by_name("wd")
  16. elem.send_keys("selenium")
  17. elem.send_keys(Keys.RETURN)
  18. assert "百度" in driver.title
  19. driver.close()
  20. driver.quit()
  21. driver = None
  22. def startFirefoxWithSpecificLocation():
  23. """启动安装在 非 默认位置的Firefox浏览器,并自动转到 百度 首页"""
  24. firefoxBin = os.path.abspath(r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
  25. os.environ["webdriver.firefox.bin"] = firefoxBin
  26. driver = webdriver.Firefox()
  27. driver.get("http://www.baidu.com")
  28. assert("百度" in driver.title)
  29. elem = driver.find_element_by_name("wd")
  30. elem.send_keys("selenium")
  31. elem.send_keys(Keys.RETURN)
  32. assert "百度" in driver.title
  33. driver.close()
  34. driver.quit()
  35. driver = None
  36. def startChrome():
  37. """启动Chrome浏览器,并自动转到 百度 首页
  38. 启动Chrome浏览器需要指定驱动的位置
  39. """
  40. chrome_driver = os.path.abspath(r"D:\云盘\360云\360云盘\我的自动双向同步文件夹\01-PersonalInfo\DataGuru\12-软件自动化测试Selenium2\1-课程\练习代码_Python版本\Selenium_python\Files\chromedriver.exe")
  41. os.environ["webdriver.chrome.driver"] = chrome_driver
  42. driver = webdriver.Chrome(chrome_driver)
  43. driver.get("http://www.baidu.com")
  44. assert("百度" in driver.title)
  45. elem = driver.find_element_by_name("wd")
  46. elem.send_keys("selenium")
  47. elem.send_keys(Keys.RETURN)
  48. assert "百度" in driver.title
  49. driver.close()
  50. driver.quit()
  51. driver = None
  52. def startIE():
  53. """启动IE浏览器,并自动转到 百度 首页
  54. 启动 IE 浏览器需要指定驱动的位置
  55. """
  56. ie_driver = os.path.abspath(r"D:\云盘\360云\360云盘\我的自动双向同步文件夹\01-PersonalInfo\DataGuru\12-软件自动化测试Selenium2\1-课程\练习代码_Python版本\Selenium_python\Files\IEDriverServer.exe")
  57. os.environ["webdriver.ie.driver"] = ie_driver
  58. driver = webdriver.Ie(ie_driver)
  59. driver.get("http://www.python.org")
  60. assert("Python" in driver.title)
  61. elem = driver.find_element_by_id("id-search-field")
  62. elem.send_keys("selenium")
  63. '''
  64. elem.send_keys(Keys.RETURN)
  65. assert "百度" in driver.title
  66. driver.close()
  67. driver.quit()
  68. driver = None
  69. '''
  70. def start_firefox_with_firebug_plug():
  71. """启动Firefox,并自动加载插件Firebug"""
  72. firefoxBin = os.path.abspath(r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
  73. os.environ["webdriver.firefox.bin"] = firefoxBin
  74. firefoxProfile = webdriver.FirefoxProfile()
  75. tempDir = os.getcwd()
  76. tempDir = os.path.split(tempDir)[0]
  77. firebugPlugFile = os.path.join(os.path.join(tempDir,"Files"), "firebug-2.0.7.xpi")
  78. firefoxProfile.add_extension(firebugPlugFile)
  79. firefoxProfile.set_preference("extensions.firebug.currentVersion", "2.0.7")
  80. driver = webdriver.Firefox(firefox_profile=firefoxProfile)
  81. driver.get("http://www.baidu.com")
  82. def start_chrome_with_chrometomobile_plug():
  83. """启动Chrome,并自动加载插件Chrome to Mobile"""
  84. tempDir = os.getcwd()
  85. tempDir = os.path.split(tempDir)[0]
  86. chrome_driver_file = os.path.join(os.path.join(tempDir,"Files"), "chromedriver.exe")
  87. os.environ["webdriver.chrome.driver"] = chrome_driver_file
  88. chrome_to_mobile_plug_file =  os.path.join(os.path.join(tempDir,"Files"), "Chrome-to-Mobile_v3.3.crx")
  89. chrome_options = webdriver.ChromeOptions()
  90. chrome_options.add_extension(chrome_to_mobile_plug_file)
  91. driver = webdriver.Chrome(executable_path=chrome_driver_file,
  92. chrome_options=chrome_options)
  93. driver.get("http://www.baidu.com")
  94. '''
  95. driver.close()
  96. driver.quit()
  97. driver = None
  98. '''
  99. def start_firefox_with_default_settings():
  100. """启动Firefox浏览器, 使用本地配置文件中的选项配置浏览器
  101. 自动将页面载入过程导出为Har文件,并存放在
  102. 配置项 extensions.firebug.netexport.defaultLogDir指定的D:\temp\selenium2目录下
  103. """
  104. firefox_bin = os.path.abspath(r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
  105. os.environ["webdriver.firefox.bin"] = firefox_bin
  106. # 使用从别的机器上拷贝来的浏览器配置
  107. firefox_profile = webdriver.FirefoxProfile(os.path.abspath(r"D:\Temp\selenium2\Profiles\mm9zxom8.default"))
  108. # 使用本地的默认配置
  109. #firefox_profile = webdriver.FirefoxProfile(r"C:\Users\eli\AppData\Roaming\Mozilla\Firefox\Profiles\mm9zxom8.default")
  110. driver = webdriver.Firefox(firefox_profile=firefox_profile)
  111. driver.get("http://www.baidu.com")
  112. driver.get("http://www.baidu.com")
  113. '''
  114. driver.close()
  115. driver.quit()
  116. driver = None
  117. '''
  118. def start_chrome_with_default_settings():
  119. """启动Firefox浏览器, 使用本地配置文件中的选项配置浏览器"""
  120. tempDir = os.getcwd()
  121. tempDir = os.path.split(tempDir)[0]
  122. chrome_driver = chrome_driver_file = os.path.join(os.path.join(tempDir,"Files"), "chromedriver.exe")
  123. os.environ["webdriver.chrome.driver"] = chrome_driver
  124. chrome_options = webdriver.ChromeOptions()
  125. chrome_options.add_argument("--test-type")
  126. #chrome_options.add_argument("user-data-dir="+os.path.abspath(r"D:\Temp\selenium2\User Data"))
  127. chrome_options.add_argument("user-data-dir="+os.path.abspath(r"C:\Users\eli\AppData\Local\Google\Chrome\User Data"))
  128. driver = webdriver.Chrome(executable_path=chrome_driver,
  129. chrome_options=chrome_options)
  130. driver.get("http://www.baidu.com")
  131. if __name__ == "__main__":
  132. # 2.启动浏览器时自动加载插件, 如Firefox -> Firebug ; Chrome -> Chrome to Mobile
  133. # start_firefox_with_firebug_plug()
  134. # start_chrome_with_chrometomobile_plug()
  135. # start_firefox_with_default_settings()
  136. start_chrome_with_default_settings()
  137. # 1.启动各种浏览器
  138. #startFirefox()
  139. #startFirefoxWithSpecificLocation()
  140. #startChrome()
  141. #startIE()

Python启动浏览器Firefox\Chrome\IE的更多相关文章

  1. selenium webdriver 启动三大浏览器Firefox,Chrome,IE

    selenium webdriver 启动三大浏览器Firefox,Chrome,IE 1.安装selenium 在联网的情况下,在Windows命令行(cmd)输入pip install selen ...

  2. selenium+python启动浏览器出错,安装浏览器驱动

    WebDriver 支持 Firefox (FirefoxDriver).IE (InternetExplorerDriver).Opera (OperaDriver) 和 Chrome (Chrom ...

  3. 【Selenium】各浏览器(firefox,chrome,ie)驱动下载地址汇总

    前两天使用Selenium分布式时,总抛出异常.更新成最新驱动可以解决.其中chrome异常如下, "platform": "WINDOWS" File &qu ...

  4. 用Python+selenium打开IE浏览器和Chrome浏览器的问题

    这几天在学Python+selenium自动化,对三大浏览器Firefox,Chrome和IE都做了尝试,也都分别下载了对应的webdriver,如:geckodriver.chromedriver. ...

  5. selenium + firefox/chrome/phantomjs登陆之模拟点击

    登陆之模拟点击 工具:python/java + selenium + firefox/chrome/phantomjs (1)windows开发环境搭建 默认已经安装好了firefox 安装pip ...

  6. Selenium2学习-005-WebUI自动化实战实例-003-三种浏览器(Chrome、Firefox、IE)启动脚本源代码

    此文主要通过 三种浏览器(Chrome.Firefox.IE)启动脚本 功能,进行 Selenium2 三种浏览器启动方法的实战实例讲解.文中所附源代码于 2015-01-18 20:33 亲测通过, ...

  7. python脚本中selenium启动浏览器报错os.path.basename(self.path), self.start_error_message) selenium.common.excep

    在python脚本中,使用selenium启动浏览器报错,原因是未安装浏览器驱动,报错内容如下: # -*- coding:utf-8 -*-from selenium import webdrive ...

  8. 各种浏览器(IE,Firefox,Chrome,Opera)COOKIE修改方法[转]

    各种浏览器(IE,Firefox,Chrome,Opera)COOKIE修改方法[转] 网站通过 Cookie 保存了我们访问网站的信息,在不同的浏览器中修改 Cookie 可以如下操作: Firef ...

  9. IE/Firefox/Chrome等浏览器保存Cookie的位置

    IE/Firefox/Chrome等浏览器保存Cookie的位置 原文  http://smilejay.com/2013/04/browser-cookie-location/   前面写了篇长文( ...

随机推荐

  1. 初级模拟电路:3-1 BJT概述

    回到目录 1.   名称由来 BJT的全称是双极性结型晶体管(Bipolar Junction Transistor),国内俗称三极管.其实,在英语中,三极管(triode)特指以前的真空电子管形式的 ...

  2. 初学者对ASCII编码、Unicode编码、UTF-8编码的理解

    最早的计算机在设计时采用8个比特(bit)作为一个字节(byte),所以,一个字节能表示的最大的整数就是 255(二进制 11111111=十进制 255),如果要表示更大的整数,就必须用更多的字节. ...

  3. 基于vue的nuxt框架cnode社区服务端渲染

    nuxt-cnode 基于vue的nuxt框架仿的cnode社区服务端渲染,主要是为了seo优化以及首屏加载速度 线上地址 http://nuxt-cnode.foreversnsd.cngithub ...

  4. 解决git pull每次提示输入账号密码的问题

    每次用git同步代码的时候,都会提示输入账号密码,很麻烦,费时间,所以找了一种可以免去每次都要输入账号密码的方法 1. git bash进入你的项目目录 2. 输入以下命令会在配置文件里添加信息,作用 ...

  5. 【codeforces 711B】Chris and Magic Square

    [题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...

  6. Hibernate基于注解annotation的配置

    Annotation在框架中是越来越受欢迎了,因为annotation的配置比起XML的配置来说方便了很多,不需要大量的XML来书写,方便简单了很多,只要几个annotation的配置,就可以完成我们 ...

  7. java 垃圾收集

    1.为什么使用垃圾收集 a.把用户从释放占用内存的重担中解救出来 b.帮助程序保持完整性 2.垃圾收集算法 检测出垃圾对象,必须回收垃圾对象所使用的堆空间并还给程序 垃圾检测:通过建立一个根对象集合并 ...

  8. [转]十五天精通WCF——第三天 client如何知道server提供的功能清单

     通常我们去大保健的时候,都会找姑娘问一下这里能提供什么服务,什么价格,这时候可能姑娘会跟你口述一些服务或者提供一份服务清单,这样的话大 家就可以做到童嫂无欺,这样一份活生生的例子,在wcf中同样是一 ...

  9. HDU 5467

    第一次写LCT,各种模板加入...以后都只遇到有新意的题目再更新了 这道题就是LCT,但是,难在一个回退的操作.这时,可以通过改变执行顺序,先把要回退后再做的操作先执行了,再回退到之前的执行.这时,建 ...

  10. ORACLE错误1033出现和ORA-00600错误解决的方法

    非法关机以后.Oracle数据常常出现这个错误: EXP-00056:ORACLE错误1033出现 ORA-01033:ORACLE initialization or shutdown in pro ...