webdriver 启动各种浏览器

  1. http://www.dataguru.cn/thread-477510-1-1.html
  2. # -*- coding:utf-8 -*-
    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. """
    def startFirefox():
  10.     """启动安装在默认位置的Firefox浏览器,并自动转到 百度 首页"""
  11.     driver = webdriver.Firefox()
  12.     driver.get(http://www.baidu.com)
  13.  
  14.    
  15. def startFirefoxWithSpecificLocation():
  16.     """启动安装在 非 默认位置的Firefox浏览器,并自动转到 百度 首页"""
  17.     firefoxBin = os.path.abspath(r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
  18.     os.environ["webdriver.firefox.bin"] = firefoxBin 
        driver = webdriver.Firefox()
  19.     driver.get(http://www.baidu.com)
  20.  
  21.    
  22. def startChrome():
  23.     """启动Chrome浏览器,并自动转到 百度 首页
  24.     启动Chrome浏览器需要指定驱动的位置
  25.     """
        chrome_driver = os.path.abspath(r"D:\云盘\360云\360云盘\Selenium2\课程\Selenium_python\Files\chromedriver.exe")
        os.environ["webdriver.chrome.driver"] = chrome_driver
  26.     driver = webdriver.Chrome(chrome_driver)
  27.     driver.get(http://www.baidu.com)
  28.  
  29.  
  30. def startIE():
  31.     """启动IE浏览器,并自动转到 百度 首页    启动 IE 浏览器需要指定驱动的位置
  32.     """
  33.     ie_driver = os.path.abspath(r"D:\云盘\360云\练习代码_Python版本\Selenium_python\Files\IEDriverServer.exe")
  34.     os.environ["webdriver.ie.driver"] = ie_driver    
  35.     driver = webdriver.Ie(ie_driver)
  36.     driver.get(http://www.python.org)
  37.     '''   
     
  38. def start_firefox_with_firebug_plug():
  39.     """启动Firefox,并自动加载插件Firebug"""
  40.     firefoxBin = os.path.abspath(r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
  41.     os.environ["webdriver.firefox.bin"] = firefoxBin   
  42.     firefoxProfile = webdriver.FirefoxProfile()
  43.     tempDir = os.getcwd()
  44.     tempDir = os.path.split(tempDir)[0]
  45.     firebugPlugFile = os.path.join(os.path.join(tempDir,"Files"), "firebug-2.0.7.xpi")   
  46.     firefoxProfile.add_extension(firebugPlugFile)
  47.     firefoxProfile.set_preference("extensions.firebug.currentVersion", "2.0.7")
  48.     driver = webdriver.Firefox(firefox_profile=firefoxProfile)
  49.     driver.get(http://www.baidu.com)
  50.  
  51. def start_chrome_with_chrometomobile_plug():
  52.     """启动Chrome,并自动加载插件Chrome to Mobile"""
  53.     tempDir = os.getcwd()
  54.     tempDir = os.path.split(tempDir)[0]
  55.     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)
  56.     driver.get(http://www.baidu.com)
       
  57.     '''

  58. def start_firefox_with_default_settings():
  59.     """启动Firefox浏览器, 使用本地配置文件中的选项配置浏览器 自动将页面载入过程导出为Har文件,并存放在
  60.     配置项 extensions.firebug.netexport.defaultLogDir指定的D:\temp\selenium2目录下    
        """
  61.     firefox_bin = os.path.abspath(r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
  62.     os.environ["webdriver.firefox.bin"] = firefox_bin   
  63.     # 使用从别的机器上拷贝来的浏览器配置
  64.     firefox_profile = webdriver.FirefoxProfile(os.path.abspath(r"D:\Temp\selenium2\Profiles\mm9zxom8.default"))
  65.     # 使用本地的默认配置
  66.     #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)
        '''
  67.  

  68. 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
  69.     chrome_options = webdriver.ChromeOptions()
  70.     chrome_options.add_argument("--test-type")
  71.     #chrome_options.add_argument("user-data-dir="+os.path.abspath(r"D:\Temp\selenium2\User Data"))
        chrome_options.add_argument("user-data-dir="+os.path.abspath(r"C:\Users\eli\AppData\Local\Google\Chrome\User Data"))
        driver = webdriver.Chrome(executable_path=chrome_driver, chrome_options=chrome_options)
  72.     driver.get(http://www.baidu.com)
  73.        
  74. if __name__ == "__main__":  
  75.     # 2.启动浏览器时自动加载插件, 如Firefox -> Firebug ; Chrome -> Chrome to Mobile
  76.     # start_firefox_with_firebug_plug()
  77.     # start_chrome_with_chrometomobile_plug()
  78.     # start_firefox_with_default_settings()
  79.     start_chrome_with_default_settings()
  80.    
  81.     # 1.启动各种浏览器
  82.     #startFirefox()
  83.     #startFirefoxWithSpecificLocation()
  84.     #startChrome()
  85.     #startIE()   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值