Last active
February 11, 2024 17:38
-
-
Save jbs1/e16b668de2830d01d851bb44f6a67550 to your computer and use it in GitHub Desktop.
Using Selenium to execute a "Click to Dial" on a Fritzbox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.Extensions.Configuration; | |
| using OpenQA.Selenium; | |
| using OpenQA.Selenium.Chrome; | |
| using OpenQA.Selenium.Support.UI; | |
| var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build(); //use dotnet user-secrets to not store credentials in code | |
| string fbLoginUrl = $"{config["Fritzbox:Url"]}"; //should be just the domain and port, without the inclusion of any path like /myfritz#/login | |
| string username = $"{config["Fritzbox:Username"]}"; | |
| string password = $"{config["Fritzbox:Password"]}"; | |
| string phonenumber = $"{config["Fritzbox:Phonenumber"]}"; | |
| ChromeOptions options = new ChromeOptions(); | |
| options.AddArgument("--headless=new"); | |
| ChromeDriver driver = new ChromeDriver(options); | |
| Console.WriteLine("Connecting to FrizBox."); | |
| driver.Url = fbLoginUrl; | |
| Console.WriteLine("Logging into FrizBox."); | |
| Dictionary<string, By> loginElements = new Dictionary<string, By> | |
| { | |
| {"usernameElement", By.Id("uiViewUser")}, | |
| {"passwordElement", By.Id("uiPassInput")}, | |
| {"loginButtonElement", By.Id("submitLoginBtn")}, | |
| }; | |
| waitForElements(loginElements, ref driver); | |
| driver.FindElement(loginElements["usernameElement"]).SendKeys(username); | |
| driver.FindElement(loginElements["passwordElement"]).SendKeys(password); | |
| driver.FindElement(loginElements["loginButtonElement"]).Click(); | |
| Console.WriteLine("Opening Telephone Book."); | |
| Dictionary<string, By> phonebookElement = new Dictionary<string, By> | |
| { | |
| {"phonebookElement", By.LinkText("Telephone Book")}, //Language specific string because I can't search by ID here | |
| }; | |
| waitForElements(phonebookElement, ref driver); | |
| driver.FindElement(phonebookElement["phonebookElement"]).Click(); | |
| Console.WriteLine($"Selecting Phone Number ({phonenumber})."); | |
| Dictionary<string, By> phonenumberElement = new Dictionary<string, By> | |
| { | |
| {"phonenumberElement", By.CssSelector($"a[data-list-item='{phonenumber}']")}, | |
| }; | |
| waitForElements(phonenumberElement, ref driver); | |
| driver.FindElement(phonenumberElement["phonenumberElement"]).Click(); | |
| Console.WriteLine($"Establishing Connection ({phonenumber})."); | |
| IAlert connectAlert = waitForAlert(ref driver); | |
| connectAlert.Accept(); //Establishing connection | |
| Console.WriteLine($"Confirming Connection ({phonenumber})."); | |
| IAlert afterConnectAlert = waitForAlert(ref driver); | |
| afterConnectAlert.Accept(); | |
| Console.WriteLine("Quitting Program."); | |
| driver.Quit(); | |
| static void waitForElements(Dictionary<string, By> elements, ref ChromeDriver driver) | |
| { | |
| WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15)); | |
| ChromeDriver localDriver = driver; //local copy of driver because passedByRef variables can't be used inside lambda function in Until() | |
| wait.Until( | |
| d => | |
| { | |
| try //have to use try/catch because WebDriverWait.IgnoreExceptionTypes simply doesn't work | |
| { | |
| foreach (var element in elements) | |
| { | |
| localDriver.FindElement(element.Value); | |
| } | |
| return true; | |
| } | |
| catch (Exception ex) when (ex is NoSuchElementException) | |
| { | |
| return false; | |
| } | |
| } | |
| ); | |
| } | |
| static IAlert waitForAlert(ref ChromeDriver driver) | |
| { | |
| WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15)); | |
| ChromeDriver localDriver = driver; //local copy of driver because passedByRef variables can't be used inside lambda function in Until() | |
| var returnedAlert = wait.Until( | |
| d => | |
| { | |
| try //have to use try/catch because WebDriverWait.IgnoreExceptionTypes simply doesn't work | |
| { | |
| IAlert alert = localDriver.SwitchTo().Alert(); | |
| return alert; | |
| } | |
| catch (Exception ex) when (ex is NoAlertPresentException) | |
| { | |
| return null; | |
| } | |
| } | |
| ); | |
| return returnedAlert; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment