简体   繁体   中英

C# Selenium Webdriver

I started using selenium with CS and have one issue. When code is compiled, program cannot find webdriver path, because it's being moved into the.exe file. I fixed this problem, by copying driver into the bin folder, so program can access it again. However, I want it to be able to access that driver inside.exe file. I was doing this in python using os path:

def resource_path(relative_path: str) -> str:
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.dirname(__file__)
    return os.path.join(base_path, relative_path)

If anyone knows how to do this in cs, please let me know. Code that I'm using in c#:

var browser = new EdgeDriver();
browser.Navigate().GoToUrl(link);

webdrivermanager should be more helpful here. you can add its Nuget and use to manage drivers for browsers without requiring the driver exe files.

I use something like this and call this method everytime I need a browser.

        public static InternetExplorerDriver InitBrowser(string browserName)
        {
            switch (browserName)
            {
                case "IE":
                    {
                        var IE_DRIVER_PATH = @"C:\PathTo\IEDriverServer";
                        InternetExplorerDriver driver = new InternetExplorerDriver(IE_DRIVER_PATH);
                        return driver;
                    }
            }
            return null;
        }

This allows you to define the path from which to grab the driver, and so you wont have to depend on it being in your BIN folder. There are other solutions but this is what I have that works really well for me. You are set up to use this method for other browsers by adding more switch cases, and also from here you can easily add your browser options. You can call the method in your tests using:

InternetExplorerDriver driver = InitBrowser(IE);

Here it is simplified without the switch case:

    var IE_DRIVER_PATH = @"C:\PathTo\IEDriverServer";
    InternetExplorerDriver driver = new InternetExplorerDriver(IE_DRIVER_PATH);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM