简体   繁体   English

C# Selenium 网络驱动程序

[英]C# Selenium Webdriver

I started using selenium with CS and have one issue.我开始将 selenium 与 CS 一起使用,但遇到一个问题。 When code is compiled, program cannot find webdriver path, because it's being moved into the.exe file.编译代码时,程序找不到 webdriver 路径,因为它正在被移动到 .exe 文件中。 I fixed this problem, by copying driver into the bin folder, so program can access it again.我通过将驱动程序复制到 bin 文件夹中解决了这个问题,因此程序可以再次访问它。 However, I want it to be able to access that driver inside.exe file.但是,我希望它能够访问 inside.exe 文件中的那个驱动程序。 I was doing this in python using os path:我在 python 中使用 os 路径执行此操作:

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.如果有人知道如何在 cs 中执行此操作,请告诉我。 Code that I'm using in c#:我在 c# 中使用的代码:

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

webdrivermanager should be more helpful here. webdrivermanager 在这里应该更有帮助。 you can add its Nuget and use to manage drivers for browsers without requiring the driver exe files.您可以添加它的 Nuget 并用于管理浏览器的驱动程序,而无需驱动程序 exe 文件。

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.这允许您定义从中获取驱动程序的路径,因此您不必依赖它在您的 BIN 文件夹中。 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.您可以通过添加更多 switch case 来设置将此方法用于其他浏览器,并且您还可以从这里轻松添加浏览器选项。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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