zoukankan      html  css  js  c++  java
  • Python启动浏览器FirefoxChromeIE

    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.     
    23. def startFirefoxWithSpecificLocation():
    24.     """启动安装在 非 默认位置的Firefox浏览器,并自动转到 百度 首页"""
    25.     firefoxBin = os.path.abspath(r"C:Program Files (x86)Mozilla Firefoxfirefox.exe")
    26.     os.environ["webdriver.firefox.bin"] = firefoxBin
    27.     
    28.     driver = webdriver.Firefox()
    29.     driver.get("http://www.baidu.com")
    30.     assert("百度" in driver.title)
    31.     elem = driver.find_element_by_name("wd")
    32.     elem.send_keys("selenium")
    33.     elem.send_keys(Keys.RETURN)
    34.     assert "百度" in driver.title
    35.     driver.close()
    36.     driver.quit()
    37.     driver = None
    38.     
    39. def startChrome():
    40.     """启动Chrome浏览器,并自动转到 百度 首页
    41.     启动Chrome浏览器需要指定驱动的位置
    42.     """
    43.     chrome_driver = os.path.abspath(r"D:云盘360云360云盘我的自动双向同步文件夹1-PersonalInfoDataGuru12-软件自动化测试Selenium21-课程练习代码_Python版本Selenium_pythonFileschromedriver.exe")
    44.     os.environ["webdriver.chrome.driver"] = chrome_driver
    45.     
    46.     driver = webdriver.Chrome(chrome_driver)
    47.     driver.get("http://www.baidu.com")
    48.     assert("百度" in driver.title)
    49.     elem = driver.find_element_by_name("wd")
    50.     elem.send_keys("selenium")
    51.     elem.send_keys(Keys.RETURN)
    52.     assert "百度" in driver.title
    53.     driver.close()
    54.     driver.quit()
    55.     driver = None
    56. def startIE():
    57.     """启动IE浏览器,并自动转到 百度 首页
    58.     启动 IE 浏览器需要指定驱动的位置
    59.     """
    60.     ie_driver = os.path.abspath(r"D:云盘360云360云盘我的自动双向同步文件夹1-PersonalInfoDataGuru12-软件自动化测试Selenium21-课程练习代码_Python版本Selenium_pythonFilesIEDriverServer.exe")
    61.     os.environ["webdriver.ie.driver"] = ie_driver
    62.     
    63.     driver = webdriver.Ie(ie_driver)
    64.     driver.get("http://www.python.org")
    65.     assert("Python" in driver.title)
    66.     elem = driver.find_element_by_id("id-search-field")
    67.     elem.send_keys("selenium")
    68.     '''
    69.     elem.send_keys(Keys.RETURN)
    70.     assert "百度" in driver.title
    71.     driver.close()
    72.     driver.quit()
    73.     driver = None    
    74.     ''' 
    75.     
    76. def start_firefox_with_firebug_plug():
    77.     """启动Firefox,并自动加载插件Firebug"""
    78.     firefoxBin = os.path.abspath(r"C:Program Files (x86)Mozilla Firefoxfirefox.exe")
    79.     os.environ["webdriver.firefox.bin"] = firefoxBin
    80.     
    81.     firefoxProfile = webdriver.FirefoxProfile()
    82.     tempDir = os.getcwd()
    83.     tempDir = os.path.split(tempDir)[0]
    84.     firebugPlugFile = os.path.join(os.path.join(tempDir,"Files"), "firebug-2.0.7.xpi")    
    85.     firefoxProfile.add_extension(firebugPlugFile)
    86.     firefoxProfile.set_preference("extensions.firebug.currentVersion", "2.0.7")
    87.     
    88.     driver = webdriver.Firefox(firefox_profile=firefoxProfile)
    89.     driver.get("http://www.baidu.com")
    90. def start_chrome_with_chrometomobile_plug():
    91.     """启动Chrome,并自动加载插件Chrome to Mobile"""
    92.     tempDir = os.getcwd()
    93.     tempDir = os.path.split(tempDir)[0]
    94.     chrome_driver_file = os.path.join(os.path.join(tempDir,"Files"), "chromedriver.exe")        
    95.     os.environ["webdriver.chrome.driver"] = chrome_driver_file
    96.     
    97.     chrome_to_mobile_plug_file =  os.path.join(os.path.join(tempDir,"Files"), "Chrome-to-Mobile_v3.3.crx")
    98.     chrome_options = webdriver.ChromeOptions()
    99.     chrome_options.add_extension(chrome_to_mobile_plug_file)
    100.     driver = webdriver.Chrome(executable_path=chrome_driver_file, 
    101.                              chrome_options=chrome_options)
    102.     driver.get("http://www.baidu.com")
    103.     '''
    104.     driver.close()
    105.     driver.quit()
    106.     driver = None
    107.     '''
    108. def start_firefox_with_default_settings():
    109.     """启动Firefox浏览器, 使用本地配置文件中的选项配置浏览器
    110.     自动将页面载入过程导出为Har文件,并存放在
    111.     配置项 extensions.firebug.netexport.defaultLogDir指定的D: empselenium2目录下    
    112.     """
    113.     firefox_bin = os.path.abspath(r"C:Program Files (x86)Mozilla Firefoxfirefox.exe")
    114.     os.environ["webdriver.firefox.bin"] = firefox_bin
    115.     
    116.     # 使用从别的机器上拷贝来的浏览器配置
    117.     firefox_profile = webdriver.FirefoxProfile(os.path.abspath(r"D:Tempselenium2Profilesmm9zxom8.default"))
    118.     # 使用本地的默认配置
    119.     #firefox_profile = webdriver.FirefoxProfile(r"C:UserseliAppDataRoamingMozillaFirefoxProfilesmm9zxom8.default")
    120.     driver = webdriver.Firefox(firefox_profile=firefox_profile)
    121.     driver.get("http://www.baidu.com")
    122.     driver.get("http://www.baidu.com")
    123.     '''
    124.     driver.close()
    125.     driver.quit()
    126.     driver = None
    127.     '''
    128. def start_chrome_with_default_settings():
    129.     """启动Firefox浏览器, 使用本地配置文件中的选项配置浏览器"""
    130.     tempDir = os.getcwd()
    131.     tempDir = os.path.split(tempDir)[0]
    132.     chrome_driver = chrome_driver_file = os.path.join(os.path.join(tempDir,"Files"), "chromedriver.exe")        
    133.     os.environ["webdriver.chrome.driver"] = chrome_driver
    134.     
    135.     chrome_options = webdriver.ChromeOptions()
    136.     chrome_options.add_argument("--test-type")
    137.     #chrome_options.add_argument("user-data-dir="+os.path.abspath(r"D:Tempselenium2User Data"))
    138.     chrome_options.add_argument("user-data-dir="+os.path.abspath(r"C:UserseliAppDataLocalGoogleChromeUser Data"))
    139.     driver = webdriver.Chrome(executable_path=chrome_driver, 
    140.                              chrome_options=chrome_options)
    141.     driver.get("http://www.baidu.com")
    142.     
    143.     
    144. if __name__ == "__main__":  
    145.     # 2.启动浏览器时自动加载插件, 如Firefox -> Firebug ; Chrome -> Chrome to Mobile
    146.     # start_firefox_with_firebug_plug()
    147.     # start_chrome_with_chrometomobile_plug()
    148.     # start_firefox_with_default_settings()
    149.     start_chrome_with_default_settings()
    150.     
    151.     
    152.     # 1.启动各种浏览器
    153.     #startFirefox()
    154.     #startFirefoxWithSpecificLocation()
    155.     #startChrome()
    156.     #startIE()   
  • 相关阅读:
    三款主流静态源代码安全检测工具比较
    原生JS封装ajax以及request
    vue生命周期
    微信 jssdk 逻辑在 vue 中的运用
    JS数组中的indexOf方法
    React和Vue中,是如何监听变量变化的
    Vue2学习小记-给Vue2路由导航钩子和axios拦截器做个封装
    VUE使用中踩过的坑
    Vue系列(一):简介、起步、常用指令、事件和属性、模板、过滤器
    Vue系列(二):发送Ajax、JSONP请求、Vue生命周期及实例属性和方法、自定义指令与过渡
  • 原文地址:https://www.cnblogs.com/my-blogs-for-everone/p/8251645.html
Copyright © 2011-2022 走看看