我试图将文件路径设置为变量,然后打开Selenium Webdriver。 文件由用户定位,因此我需要将此值作为变量,而不是原始粘贴文件路径。
当我使用此功能时:
FirefoxDriverService service =
FirefoxDriverService.CreateDefaultService(@"C:\...\Geckodriver);
service.FirefoxBinaryPath = @"C:\Program Files\Mozilla Firefox\firefox.exe";
一切都很好,但是当我用变量替换路径时,它不再起作用。
private void Form2_Load(object sender, EventArgs e)
{
using (IsolatedStorageFile iso = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
{
try
{
using (IsolatedStorageFileStream isos = new IsolatedStorageFileStream("firefoxpath.txt", System.IO.FileMode.Open, iso))
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(isos))
{
fpath = sr.ReadLine();
}
}
}
catch { }
}
using (IsolatedStorageFile iso = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
{
try
{
using (IsolatedStorageFileStream isos = new IsolatedStorageFileStream("gecopath.txt", System.IO.FileMode.Open, iso))
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(isos))
{
gpath = sr.ReadLine();
}
}
}
catch { }
}
}
string fpath;
string gpath;
private void button1_Click(object sender, EventArgs e)
{
string elo;
string byebye;
elo = "@\"xd\"".Replace("xd", gpath);
byebye = "@\"xd\"".Replace("xd", fpath);
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(elo);
service.FirefoxBinaryPath = byebye;
IWebDriver driver = new FirefoxDriver(service);
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
}
}
}
答案 0 :(得分:0)
将Selenium与C#一起使用时,并且如果您的项目包含NuGet程序包管理器依赖关系,则无需在脚本或系统环境中为Chrome / Firefox / Edge定义任何路径。
驱动程序实用程序将在内部调用。
对于Firefox:NuGet程序包管理器中的Selenium.Firefox.WebDriver