启动不同的浏览器函数封装

启动浏览器一般这么写:

from selenium import webdriver
driver = webdriver.Chrome() # driver = webdriver.Ie() # driver = webdriver.Firefox()

  

简单写个启动浏览器函数,将已经在本机安装过驱动的浏览器写在函数里:

#coding:utf-8
"""
启动浏览器

"""

from selenium import webdriver

def launch_browser(browsername):

    if browsername.lower() == "chrome":
        driver = webdriver.Chrome()

    elif browsername.lower() == "firefox":
        driver = webdriver.Firefox()

    elif browsername.lower() == 'ie':
        driver = webdriver.Ie()

    else:
        raise Exception("sorry,该浏览器不在启动范围内,请安装驱动并修改launch_browser函数")

    return driver

  

使用: 

driver = launch_browser('chrome') # 启动chrome浏览器

driver = launch_browser('ie') # 启动ie浏览器

driver = launch_browser('firefox') # 启动火狐浏览器

 

 

driver = launch_browser('safari') #启动未安装驱动的浏览器

  

 

posted @ 2019-01-09 19:43  dinghanhua  阅读(430)  评论(0编辑  收藏  举报