How to make a basic Chrome Selenium program C#

[By JayDev64x] [Index]

Here are some core script examples of the very basic functions selenium is capable of. In this example it shows how to navigate to a url (site), click a button/enter in an input using XPath to find the elements, and then finally closes both the chrome window and console window.

using System; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using System.Threading; namespace Script { class Example { // Put the path of your chrome driver here. IWebDriver driver = new ChromeDriver(DRIVERPATH); void Start() { // Navigates to the URL you want to be on, which in this example is this site. driver.Navigate().GoToUrl("https://bitbox.pw/test.html"); // Waits one second for the site elements to be loaded. Thread.Sleep(1000); // Clicks the button on the site. driver.FindElement(By.XPath("/html/body/button")).Click(); // Enters text into the input on the site. driver.FindElement(By.XPath("/html/body/input")).SendKeys("Selenium is awesome :D"); // Waits ten seconds and then closes down the driver and program Thread.Sleep(10000); driver.Close(); Environment.Exit(0); } // Take the program out of a static loop to use the driver variable. static void Main() { Example example = new Example(); example.Start(); } } }

Created [September 2, 2021]