Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[How To] [C#] Dynamically download Edge Webdriver #40

Open
miksch123 opened this issue Mar 26, 2021 · 3 comments
Open

[How To] [C#] Dynamically download Edge Webdriver #40

miksch123 opened this issue Mar 26, 2021 · 3 comments

Comments

@miksch123
Copy link

Hello,
This is not an issue but rather a way to dynamically download the correct Edge webdriver depending on what edge version is currently installed on your system.
I hope some of you might find this helpful.
The same can be donw for Mac OS and Linux. I just wanted to leave a bit of an inspiration.

public void DownloadEdgeDriver()
        {
            FileVersionInfo info = null;
            string version = null;
            string drivername = null;
            if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                if(!File.Exists(tmpfolder + "edgeversion.txt"))
                    File.Create(tmpfolder  + "edgeversion.txt").Close();

                if(Environment.Is64BitOperatingSystem is true)
                {
                    info = FileVersionInfo.GetVersionInfo(@"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe");
                    drivername = "edgedriver_win64.zip";
                }
                else
                {
                    info = FileVersionInfo.GetVersionInfo(@"C:\Program Files\Microsoft\Edge\Application\msedge.exe");
                    drivername = "edgedriver_win32.zip";
                }
                
                version = info.FileVersion;
                string current = File.ReadAllText(tmpfolder  + "edgeversion.txt").ToString();
                if (version != current)
                {
                    if(File.Exists(driverfolder + "\\*.zip"))
                        File.Delete(driverfolder  + "\\*.zip");
                    if(File.Exists(driverfolder  + "\\msedgedriver.exe"))
                        File.Delete(driverfolder  + "\\msedgedriver.exe");            
                    
                    using (var client = new WebClient()) 
                    {
                        client.DownloadFile("https://msedgedriver.azureedge.net/" + version + "/" + drivername, driverfolder  + "\\" + drivername);
                    }
                    if(File.Exists(driverfolder   + "\\msedgedriver.exe"))
                        File.Delete(driverfolder   + "\\msedgedriver.exe");
                    if(Directory.Exists(driverfolder   + "\\Driver_Notes"))
                        Directory.Delete(driverfolder   + "\\Driver_Notes", true);
                    ZipFile.ExtractToDirectory(driverfolder   + "\\" + drivername, Configuration.DefaultBinDirWin);
                    File.WriteAllText(tmpfolder + "edgeversion.txt", version);
                }
            }
        }
@zaphodikus
Copy link

if using python bindings, you can read up https://pypi.org/project/webdriver-manager/
pip install webdriver-manager

then, in code
from webdriver_manager.microsoft import EdgeChromiumDriverManager
mgr = EdgeChromiumDriverManager()
driver_path = mgr.install()
EdgeDriverWrapper._driver = Edge(executable_path=driver_path)

literally you can do this in 2 lines with that module

@kwiremo
Copy link

kwiremo commented Oct 29, 2021

Thank you @miksch123. This was very helpful.

@bwalderman
Copy link
Contributor

Thank you @miksh123. The repo is now deprecated but I'm leaving this issue open with the conversation locked so that people can still find this sample.

@microsoft microsoft locked as resolved and limited conversation to collaborators Jan 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants