当前目录的问题

原来写了个用到Selenium web driver的程序,里面有这么一句:

var driver = new ChromeDriver(".\\", options, TimeSpan.FromSeconds(180));

本来运行正常。但是昨天从vbscript里调用它,却出错了,提示找不到chromedriver.exe。

vbscript程序

             Set shl = CreateObject("Wscript.Shell")  
             Call shl.Run("""foo.exe""") 

原因是从vbscript调用时,当前目录变成了vbscript所在的目录。

解决办法有两种。一种是修改vbscript:

             Set shl = CreateObject("Wscript.Shell")  
             shl.CurrentDirectory = <foo.exe所在目录>
             Call shl.Run("""foo.exe""") 

另一种是修改C#代码:

var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var driver = new ChromeDriver(dir, options, TimeSpan.FromSeconds(180));

教训是用到当前目录时,尽量避免用.\\这种写法,因为不能确定程序是从何处调用的。

posted @ 2023-04-23 20:46  平静寄居者  阅读(13)  评论(0编辑  收藏  举报