Python启动浏览器Firefox\Chrome\IE

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

原文地址:https://www.cnblogs.com/my-blogs-for-everone/p/8251645.html

时间: 2024-08-03 08:15:35

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

关于浏览器的选择 360浏览器 firefox chrome

对于浏览器的选择,一直比较纠结. 我最关心的就是浏览器的书签,浏览各种技术网页时顺便就收藏到了书签,以备查找 开始用了好多浏览器,最终确定用360急速浏览器,一下简称360. 原因: 1.360做的很漂亮,对比其他浏览器,有好多主题和皮肤,用户有很多选择,很容易找到自己喜欢的. 2.360用的是Chrome和IE的双内核,运行速度比较快,不知是不是错觉,在启动时总感觉360比较快,轻巧.而启动Chrome和Firefox时就感觉很笨拙. 3.360的书签管理和同步做的很好,在我开始选择用360时

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

WebDriver 支持 Firefox (FirefoxDriver).IE (InternetExplorerDriver).Opera (OperaDriver) 和 Chrome (ChromeDriver) . 下载ChromeDriver.exe.IEDriverServer.exe放到D:\python27 执行: # coding = utf-8from selenium import webdriverbrowser = webdriver.Chrome()browser.ge

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

此文主要通过 三种浏览器(Chrome.Firefox.IE)启动脚本 功能,进行 Selenium2 三种浏览器启动方法的实战实例讲解.文中所附源代码于 2015-01-18 20:33 亲测通过,敬请亲们阅览.进行编写登录自动化测试脚本,若您直接使用此文所附的源代码运行测试,则需要修改对应 浏览器 或 webdriver 的路径,否则将会引起相应的报错,请知悉. 希望能对初学 Selenium2 WebUI 自动化测试编程的亲们有所帮助.若有不足之处,敬请大神指正,不胜感激! 一.各浏览器

Python+selenium+eclipse执行web自动化(二)启动浏览器

一.浏览器兼容性 当前selenium 2.0支持的浏览器 从selenium的__init__文件中可以查看到,我当前使用的selenium版本是2.53.2 从selenium的Webdriver模块查看__init__文件,可以看到以下代码: from .firefox.webdriver import WebDriver as Firefoxfrom .firefox.firefox_profile import FirefoxProfilefrom .chrome.webdriver

PDF在线阅读控件多浏览器(IE,firefox,chrome,opera等)中的使用

iStylePDF是基于ActiveX技术开发的一个COM组件,一般是应用于IE浏览器中来使用,但是随着越来越多的浏览器出现,导致客户端的环境非常复杂,客户的需求又是多样化的,所以我们这里介绍了一种更快的让我们的COM组件在各种浏览器中通行无阻的方法. 浏览器现在的主流一般分为IE内核和非IE内核浏览器,IE浏览器中的使用无需太多的说明,直接应用object对象就可以使用了. 如图所示: 启动页面的时候判断下浏览器类型 非IE内核浏览器基本上都支持NPAPI插件模式,我们提供了一个简单的控件注册

JS完美识别IE, firefox, chrome, safari浏览器

(转)判断是否为IE浏览器很简单,用navigator.appName来判断就行了.而Firefox, Chrome, Safari的navigator.appName输出值都是"Netscape",现在我们用navigator.userAgent这个属性来判断,因为谷歌浏览器Chrome的navigator.userAgent值包含"Safari"字符串而Safari浏览器并不包含Chrome,因此我们可以用这个区别来区分safari和chrome. 完整代码如下

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

IE/Firefox/Chrome等浏览器保存Cookie的位置 原文  http://smilejay.com/2013/04/browser-cookie-location/ 前面写了篇长文( 使用Jmeter登录WordPress的问题 )中也重点是Cookie的问题,这里再简单说下什么是Cookie并且列举一下主流浏览器保存Cookie的位置吧. 什么是Cookie? A cookie, also known as an HTTP cookie, web cookie, or brows

如何将firefox,Chrome导出的html格式书签导入IE浏览器

将Firefox书签导入IE很简单: 在Firefox的书签 - 管理书签 - 文件菜单 - 导出 -导出Firefox收藏夹文件,是个Html文件:(Chrome的到处与此类似) 用记事本打开导出的书签文件 - 另存为 - 编码选ANSI(其它不变).注意,仍要保存为HTML格式! 打开IE,文件 菜单 - 导入和导出 - 选择导入收藏夹 - 从文件导入 - 选择刚才你修改编码为ANSI的HTMl文件 - 导入成功.(或许你需要按Alt键使菜单栏显现出来) 另,修改IE收藏夹位置: 默认情况下

div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法(非原创)

div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法 1.DOCTYPE 影响 CSS 处理 2.FF: div 设置 margin-left, margin-right 为 auto 时已经居中, IE 不行 3.FF: body 设置 text-align 时, div 需要设置 margin: auto(主要是 margin-left,margin-right) 方可居中 4.FF: 设置 padding 后, div 会增加 height 和 wi