关于UI自动化浏览器驱动路径设置

    xiaoxiao2024-11-03  73

    做UI自动化测试需要用到浏览器驱动,现在高版本的浏览器都要带驱动,只有低版本selenium2.48+火狐35才免驱。那么问题来了,selenium版本过低有时候兼容性就差。

    一、使用高版本的时候需要带驱动,通常将驱动放到项目结构下的文件夹中,最简单的设置方式:

    driver = webdriver.Chrome(executable_path=r'D:\项目名称\driver\chromedriver.exe') driver.get('https://mail.163.com/')

    二、第二种写法:

    i_path = os.getcwd() #获取当前文件路径 cur_path = os.path.dirname(i_path) #当前文件上一层路径 web_path = os.path.join(cur_path+'\\'+"driver"+'\\'+"chromedriver.exe") print(web_path) driver = webdriver.Chrome(executable_path=web_path) driver.get('https://mail.163.com/')

    三、第三种方法比较正规,符合整个项目结构和环境考虑。 1.首先要写一个专门读取配置文件ini的py文件。

    import configparser import codecs class ReadConfig: """ 专门读取配置文件的,.ini文件格式 """ def __init__(self, filename): configpath = filename fd = open(configpath) data = fd.read() if data[:3] == codecs.BOM_UTF8: data = data[3:] files = codecs.open(configpath, "w") files.write(data) files.close() fd.close() self.cf = configparser.ConfigParser() self.cf.read(configpath) def getValue(self, env, name): #该方法返回的是项目路径 return self.cf.get(env,name)

    配置文件:

    其中projectConfig就是配置文件中的[projectConfig],如果需要多个,也可以配置。

    火狐浏览器历史版本驱动:http://ftp.mozilla.org/pub/firefox/releases/ 谷歌驱动:http://chromedriver.storage.googleapis.com/index.html IE驱动:http://selenium-release.storage.googleapis.com/index.html

    最新回复(0)