一、使用高版本的时候需要带驱动,通常将驱动放到项目结构下的文件夹中,最简单的设置方式:
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],如果需要多个,也可以配置。
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理