Best WinAppDriver code snippet using WebDriverAPI.Element
SimpleAttributeExpression.cs
Source:SimpleAttributeExpression.cs
...27 try28 {29 // [@Name=\"St^*~art\"]30 var xpath = "/Pane[@ClassName=\"#32769\"][@Name=\"Desktop 1\"]/Pane[@ClassName=\"Shell_TrayWnd\"][@Name=\"Taskbar\"]/Button[@ClassName=\"Start\"][@Name=\"St^*~art\"]";31 desktopSession.DesktopSessionElement.FindElementByXPath(xpath);32 Assert.Fail("Exception should have been thrown");33 }34 catch (InvalidOperationException exception)35 {36 Assert.AreEqual(WebDriverAPI.ErrorStrings.NoSuchElement, exception.Message);37 }38 }39 [TestMethod]40 public void NoAttributeValue()41 {42 // [@Name]43 var xpath = "/Pane[@ClassName=\"#32769\"][@Name=\"Desktop 1\"]/Pane[@ClassName=\"Shell_TrayWnd\"][@Name=\"Taskbar\"]/Button[@ClassName=\"Start\"][@Name]";44 Assert.IsNotNull(desktopSession.DesktopSessionElement.FindElementByXPath(xpath));45 }46 [TestMethod]47 public void InvalidAttributeName()48 {49 try50 {51 // [@ClassNameInvalid=\"Shell_TrayWnd\"]52 var xpath = "/Pane[@ClassName=\"#32769\"][@Name=\"Desktop 1\"]/Pane[@ClassNameInvalid=\"Shell_TrayWnd\"][@Name=\"Taskbar\"]/Button[@ClassName=\"Start\"][@Name=\"Start\"]";53 desktopSession.DesktopSessionElement.FindElementByXPath(xpath);54 Assert.Fail("Exception should have been thrown");55 }56 catch (InvalidOperationException exception)57 {58 Assert.AreEqual(WebDriverAPI.ErrorStrings.NoSuchElement, exception.Message);59 }60 }61 [TestMethod]62 public void InvalidTagName()63 {64 try65 {66 // InvalidPane67 var xpath = "/Pane[@ClassName=\"#32769\"][@Name=\"Desktop 1\"]/InvalidPane[@ClassName=\"Shell_TrayWnd\"][@Name=\"Taskbar\"]/Button[@ClassName=\"Start\"][@Name=\"Start\"]";68 desktopSession.DesktopSessionElement.FindElementByXPath(xpath);69 Assert.Fail("Exception should have been thrown");70 }71 catch (InvalidOperationException exception)72 {73 Assert.AreEqual(WebDriverAPI.ErrorStrings.NoSuchElement, exception.Message);74 }75 }76 [TestMethod]77 public void NameContainsStart()78 {79 var xpath = "/Pane[@ClassName=\"#32769\"][@Name=\"Desktop 1\"]/Pane[@ClassName=\"Shell_TrayWnd\"][@Name=\"Taskbar\"]/Button[contains(@Name,\"Start\")]";80 Assert.IsNotNull(desktopSession.DesktopSessionElement.FindElementByXPath(xpath));81 }82 [TestMethod]83 public void ClassNameStartsWithStart()84 {85 var xpath = "/Pane[@ClassName=\"#32769\"][@Name=\"Desktop 1\"]/Pane[starts-with(@ClassName,\"Shell_TrayWnd\")][@Name=\"Taskbar\"]/Button[contains(@Name,\"Start\")]";86 Assert.IsNotNull(desktopSession.DesktopSessionElement.FindElementByXPath(xpath));87 }88 [TestMethod]89 public void AttributesInSingleQuote()90 {91 var xpath = "/Pane[@ClassName=\"#32769\"][@Name=\"Desktop 1\"]/Pane[starts-with(@ClassName,'Shell_TrayWnd')][@Name='Taskbar']/Button[contains(@Name,'Start')]";92 Assert.IsNotNull(desktopSession.DesktopSessionElement.FindElementByXPath(xpath));93 }94 [TestMethod]95 public void ButtonByPosition()96 {97 string xpath = "/Pane[@ClassName=\"#32769\"][@Name=\"Desktop 1\"]/Pane[@ClassName=\"Shell_TrayWnd\"][@Name=\"Taskbar\"]/Button[position()=1]";98 Assert.IsNotNull(desktopSession.DesktopSessionElement.FindElementByXPath(xpath));99 }100 }101}
Element
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Chrome;3using OpenQA.Selenium.Support.UI;4using System;5using System.Collections.Generic;6using System.Linq;7using System.Text;8using System.Threading.Tasks;9{10 {11 static void Main(string[] args)12 {13 IWebDriver driver = new ChromeDriver();14 IWebElement searchBox = driver.FindElement(By.Id("lst-ib"));15 searchBox.SendKeys("Selenium WebDriver");16 IWebElement searchButton = driver.FindElement(By.Name("btnK"));17 searchButton.Click();18 driver.Quit();19 }20 }21}22using OpenQA.Selenium;23using OpenQA.Selenium.Chrome;24using OpenQA.Selenium.Support.UI;25using System;26using System.Collections.Generic;27using System.Linq;28using System.Text;29using System.Threading.Tasks;30{31 {32 static void Main(string[] args)33 {34 IWebDriver driver = new ChromeDriver();35 IWebElement searchBox = driver.FindElement(By.Id("lst-ib"));36 searchBox.SendKeys("Selenium WebDriver");37 IWebElement searchButton = driver.FindElement(By.Name("btnK"));38 searchButton.Click();39 driver.Quit();40 }41 }42}43using OpenQA.Selenium;44using OpenQA.Selenium.Chrome;45using OpenQA.Selenium.Support.UI;46using System;47using System.Collections.Generic;48using System.Linq;49using System.Text;50using System.Threading.Tasks;51{52 {53 static void Main(string[] args)54 {55 IWebDriver driver = new ChromeDriver();56 IWebElement searchBox = driver.FindElement(By.Id("lst-ib"));57 searchBox.SendKeys("Selenium WebDriver");58 IWebElement searchButton = driver.FindElement(By.Name("btnK"));59 searchButton.Click();60 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));61 wait.Until(ExpectedConditions.ElementExists(By.PartialLinkText("Selenium WebDriver")));62 IWebElement link = driver.FindElement(By.PartialLinkText("Selenium WebDriver"));63 link.Click();64 driver.Quit();65 }66 }67}
Element
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Firefox;3using OpenQA.Selenium.Support.UI;4using System;5using System.Collections.Generic;6using System.Linq;7using System.Text;8using System.Threading.Tasks;9{10 {11 static void Main(string[] args)12 {13 IWebDriver driver = new FirefoxDriver();14 IWebElement searchBox = driver.FindElement(By.Id("lst-ib"));15 searchBox.SendKeys("Selenium");16 IWebElement searchButton = driver.FindElement(By.Name("btnK"));17 searchButton.Click();18 System.Threading.Thread.Sleep(5000);19 driver.Close();20 }21 }22}23using OpenQA.Selenium;24using OpenQA.Selenium.Firefox;25using OpenQA.Selenium.Support.UI;26using System;27using System.Collections.Generic;28using System.Linq;29using System.Text;30using System.Threading.Tasks;31{32 {33 static void Main(string[] args)34 {35 IWebDriver driver = new FirefoxDriver();36 IWebElement searchBox = driver.FindElement(By.Id("lst-ib"));37 searchBox.SendKeys("Selenium");38 IWebElement searchButton = driver.FindElement(By.Name("btnK"));39 searchButton.Click();40 System.Threading.Thread.Sleep(5000);41 driver.Close();42 }43 }44}45using OpenQA.Selenium;46using OpenQA.Selenium.Firefox;47using OpenQA.Selenium.Support.UI;48using System;
Element
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Firefox;3using OpenQA.Selenium.Support.UI;4using System;5{6 {7 static void Main(string[] args)8 {9 IWebDriver driver = new FirefoxDriver();10 IWebElement element = driver.FindElement(By.LinkText("Download"));11 element.Click();12 System.Threading.Thread.Sleep(5000);13 driver.Quit();14 }15 }16}17The following are the important points to remember while using the LinkText method of By class:
Element
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium;3using OpenQA.Selenium.Chrome;4using OpenQA.Selenium;5using System.Threading;6using OpenQA.Selenium;7using OpenQA.Selenium.Support.UI;8using OpenQA.Selenium;9using OpenQA.Selenium.Support.UI;10using OpenQA.Selenium;11using OpenQA.Selenium;12using OpenQA.Selenium;13using System.Threading;14using OpenQA.Selenium;15using OpenQA.Selenium.Support.UI;16using OpenQA.Selenium;17using OpenQA.Selenium.Support.UI;18using OpenQA.Selenium;19using OpenQA.Selenium;20using OpenQA.Selenium;21using System.Threading;22using OpenQA.Selenium;23using OpenQA.Selenium.Support.UI;24using OpenQA.Selenium;25using OpenQA.Selenium.Support.UI;26using OpenQA.Selenium;27using OpenQA.Selenium;28using OpenQA.Selenium;
Element
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Chrome;3using System;4{5 {6 static void Main(string[] args)7 {8 IWebDriver driver = new ChromeDriver();9 IWebElement element = driver.FindElement(By.Name("q"));10 element.SendKeys("Selenium");11 IWebElement submit = driver.FindElement(By.Name("btnK"));12 submit.Submit();13 System.Threading.Thread.Sleep(5000);14 driver.Close();15 }16 }17}18using OpenQA.Selenium;19using OpenQA.Selenium.Chrome;20using System;21{22 {23 static void Main(string[] args)24 {25 IWebDriver driver = new ChromeDriver();26 IWebElement element = driver.FindElement(By.Name("q"));27 element.SendKeys("Selenium");28 IWebElement submit = driver.FindElement(By.Name("btnK"));29 submit.Submit();30 System.Threading.Thread.Sleep(5000);31 driver.Close();32 }33 }34}35using OpenQA.Selenium;36using OpenQA.Selenium.Chrome;37using System;38{39 {40 static void Main(string[] args)41 {42 IWebDriver driver = new ChromeDriver();43 IWebElement element = driver.FindElement(By.Name("q"));44 element.SendKeys("Selenium");45 IWebElement submit = driver.FindElement(By.Name("btnK"));46 submit.Submit();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!