Created
August 3, 2022 04:12
-
-
Save bharathmuddada/d2ae2a39e1051d73ff3818673744a5ab to your computer and use it in GitHub Desktop.
Selenium C# Program with all Locators
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 OpenQA.Selenium; | |
using OpenQA.Selenium.Chrome; | |
using OpenQA.Selenium.Edge; | |
using WebDriverManager; | |
using WebDriverManager.DriverConfigs.Impl; | |
using OpenQA.Selenium.Support.UI; | |
namespace LearnSelenium { | |
public class SeleniumAllLocators { | |
public static void Main(string[] args) { | |
new DriverManager().SetUpDriver(new ChromeConfig()); | |
var driver = new ChromeDriver(); | |
driver.Navigate().GoToUrl("https://www.saucedemo.com/"); | |
driver.Manage().Window.Maximize(); | |
var username = driver.FindElement(By.Id("user-name")); | |
username.SendKeys("standard_user"); | |
var password = driver.FindElement(By.Name("password")); | |
password.SendKeys("secret_sauce"); | |
/* | |
* id | |
* name | |
* classname | |
* tagname | |
* link text | |
* partial link text | |
* css selector | |
* xpath | |
*/ | |
var loginbutton = driver.FindElement(By.ClassName("submit-button")); | |
loginbutton.Click(); | |
var prodtitle = driver.FindElement(By.TagName("span")); | |
Console.WriteLine($"span text is {prodtitle.Text}"); | |
Console.WriteLine(driver.FindElement(By.LinkText("LinkedIn")).Text); | |
Console.WriteLine(driver.FindElement(By.PartialLinkText("Link")).Text); | |
driver.FindElement(By.CssSelector("a[id='item_4_title_link']")).Click(); | |
//classname | |
Console.WriteLine(driver.FindElement(By.CssSelector(".inventory_details_price")).Text); | |
//substring | |
Console.WriteLine(driver.FindElement(By.CssSelector("button[data-test ^='add']")).Text); | |
Console.WriteLine(driver.FindElement(By.CssSelector("button[data-test $='backpack']")).Text); | |
Console.WriteLine(driver.FindElement(By.CssSelector("button[data-test *='sauce']")).Text); | |
//nth-child | |
Console.WriteLine(driver.FindElement(By.CssSelector("li:nth-child(2)")).Text); | |
/*cssselector: | |
* tagName[attribute=value] | |
* | |
*/ | |
//xpath | |
//tagName[@attribute=value] | |
driver.FindElement(By.XPath("//button[@id='add-to-cart-sauce-labs-backpack']")).Click(); | |
driver.FindElement(By.XPath("//a[@class='shopping_cart_link']")).Click(); | |
driver.FindElement(By.XPath("//button[@class='btn btn_action btn_medium checkout_button' " + | |
"or @id='checkout']")).Click(); | |
driver.FindElement(By.XPath("//input[starts-with(@class,'input_error') and @id='first-name']")) | |
.SendKeys("Test"); | |
driver.FindElement(By.XPath("//input[starts-with(@class,'input_error') and @id='last-name']")) | |
.SendKeys("engineer"); | |
driver.FindElement(By.XPath("//div[@class='form_group']//descendant::input[@id='postal-code']")) | |
.SendKeys("600090"); | |
driver.FindElement(By.XPath("//input[@type='submit']")).Click(); | |
driver.FindElement(By.XPath("//button[text()='Finish']")).Click(); | |
var ordermessage =driver.FindElement(By.XPath("//h2[text()='THANK YOU FOR YOUR ORDER']")); | |
Console.WriteLine(ordermessage.Displayed); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment