自动化测试常用脚本-启动不同浏览器前判断浏览器类型

import os
from selenium import webdriver

@staticmethod
def browser(browserType):
    """
    切换浏览器前,判断浏览器类型,并返回相应的驱动
    :param browserType: 浏览器类型:ie,chrome,firefox
    :return: 浏览器驱动
    """
    try:
        system_user = os.environ.get("USERNAME")
        if browserType == "firefox":
            driver = webdriver.Firefox()
            return driver
        elif browserType == "chrome":
            # # 用户数据目录
            # user_data_dir = r'--user-data-dir=C:\Users\%s\AppData\Local\Google\Chrome\User Data' % system_user
            # # 谷歌启动的位置
            # driver_path = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
            # option = webdriver.ChromeOptions()
            # option.add_argument(user_data_dir)
            # driver_path, options=option

            driver = webdriver.Chrome()
            return driver
        elif browserType == "ie":
            '''InPrivate'''
            # user_data_dir = r'--disk-cache-dir=C:\Users\%s\AppData\Local\Microsoft\Windows\Temporary Internet Files' % system_user
            option = webdriver.IeOptions()
            option.set_capability("IE_SWITCHES", "-private")
            option.set_capability("INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS", True)
            # option.add_argument('-private')
            driver = webdriver.Ie(options=option)
            return driver
        else:
            print("没有找到浏览器!你可以输入“firefox”、“chrome”、“ie”或“phantomjs”")
    except Exception as msg:
        print("打开浏览器失败:%s" % msg)
posted @ 2019-09-28 23:43  chenzy01  阅读(432)  评论(0编辑  收藏  举报