C#selenium无法从目录中找到webdriver

C#selenium无法从目录中找到webdriver,c#,selenium,selenium-webdriver,C#,Selenium,Selenium Webdriver,我无法让selenium找到我的Web驱动程序,无论是firefox还是chrome。我尝试过手动下载dirver,并使用NuGet软件包管理器,这两个版本在C#中都不适合我,但在python中,它们工作得很好 不起作用的C代码。给出错误: Exception thrown: 'OpenQA.Selenium.DriverServiceNotFoundException' in WebDriver.dll An exception of type 'OpenQA.Selenium.DriverS

我无法让selenium找到我的Web驱动程序,无论是firefox还是chrome。我尝试过手动下载dirver,并使用NuGet软件包管理器,这两个版本在C#中都不适合我,但在python中,它们工作得很好

不起作用的C代码。给出错误:

Exception thrown: 'OpenQA.Selenium.DriverServiceNotFoundException' in WebDriver.dll
An exception of type 'OpenQA.Selenium.DriverServiceNotFoundException' occurred in WebDriver.dll but was not handled in user code
The file C:/webdrivers/geckodriver.exe does not exist. The driver can be downloaded at https://github.com/mozilla/geckodriver/releases
在一些旧的stackoverflow问题上找到的逗号代码也不起作用

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;    
class Scraper
    {
        /*
        private static IWebDriver Driver { get; set; }
        public static IWebDriver Init()
        {
            //System.Environment.SetEnvironmentVariable("webdriver.gecko.driver", @"C:\webdrivers\geckodriver.exe");
            FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"C:\webdrivers");
            service.FirefoxBinaryPath = @"C:\Program Files\Mozilla Firefox\firefox.exe"; // May not be necessary
            FirefoxOptions options = new FirefoxOptions();
            TimeSpan time = TimeSpan.FromSeconds(10);
            Driver = new FirefoxDriver(service, options, time);
            return Driver;
        }
        */
        IWebDriver driver;
        public void StartScraping()
        {
            //driver = Init();
            driver = new FirefoxDriver(@"C:/webdrivers/");
        }
    }
从UWP应用程序MainPage.xaml.cs构造函数调用StartScraping()

工作正常的Python代码

import selenium
from selenium.webdriver import Firefox, Chrome


def startf():
    driverf = Firefox("C:/webdrivers/")
    driverf.get("https://www.google.com")


startf()
有人说我应该将webdrivers目录添加到PATH,我也这么做了,但没有任何改变

编辑: 在几天后回到这个问题并使用一个控制台应用程序测试selenium之后,它在那里正常工作。
这似乎是我使用空白的通用Windows平台应用程序而不是控制台应用程序的问题。有没有办法在UWP应用程序中“附加”或打开控制台窗口?或者这能解决这个问题吗?如果没有,我是否可以从控制台应用程序中打开类似的XML表单窗口?

解决此问题的最佳方法是在路径中使用
Path.DirectorySeparatorChar
,而不是使用
/
\

您可以像这样使用它:

separator = Path.DirectorySeparatorChar;
path = $"C:{separator}webdrivers{separator}";

现在,您可以在运行firefox时使用此路径。

不适用于:char separator=path.directorySpeparatorChar;driver=newfirefoxdriver($“C:{separator}webdrivers{separator}”);driver.Url=“”;还是一样的吗error@Lauri尝试使用
driver=newfirefoxdriver(@“C:\\webdrivers\\”)
并让我知道这是否有效。它基本上使用两个向后斜杠,而不是一个向前斜杠slash@Lauri您使用的是哪种系统?Windows linux还是mac?我正在使用Windows。