Best WinAppDriver code snippet using WebDriverAPI.ElementAttribute
ElementAttribute.cs
Source:ElementAttribute.cs
...17using System;18namespace WebDriverAPI19{20 [TestClass]21 public class ElementAttribute : AlarmClockBase22 {23 [ClassInitialize]24 public static void ClassInitialize(TestContext context)25 {26 Setup(context);27 }28 [ClassCleanup]29 public static void ClassCleanup()30 {31 TearDown();32 }33 [TestMethod]34 public void GetElementAttribute()35 {36 // NOTE: The attributes below are only a subset of supported attributes.37 // Use inspect.exe to identify all available attributes of an element38 var element = alarmTabElement;39 // Fixed value string attributes40 Assert.IsTrue(element.GetAttribute("Name").StartsWith("Alarm")); // name is Alarm or Alarm tab on the older version41 Assert.IsTrue(element.GetAttribute("LegacyName").StartsWith("Alarm")); // Shows as Legacy|Accessible.Name in inspect.exe42 Assert.AreEqual(element.GetAttribute("AutomationId"), AlarmTabAutomationId);43 Assert.AreEqual(element.GetAttribute("FrameworkId"), "XAML");44 Assert.AreEqual(element.GetAttribute("ClassName"), AlarmTabClassName);45 // Fixed value boolean attributes46 Assert.AreEqual(element.GetAttribute("IsEnabled"), "True");47 Assert.AreEqual(element.GetAttribute("IsKeyboardFocusable"), "True");48 Assert.AreEqual(element.GetAttribute("IsControlElement"), "True");49 // Arbitrary value attributes50 Assert.IsTrue(Convert.ToInt32(element.GetAttribute("ProcessId")) > 0);51 Assert.IsFalse(string.IsNullOrEmpty(element.GetAttribute("RuntimeId")));52 // Arbitrary value array attributes53 Assert.IsFalse(string.IsNullOrEmpty(element.GetAttribute("ClickablePoint")));54 var boundingRectangle = element.GetAttribute("BoundingRectangle");55 Assert.IsTrue(boundingRectangle.Contains("Top"));56 Assert.IsTrue(boundingRectangle.Contains("Left"));57 Assert.IsTrue(boundingRectangle.Contains("Width"));58 Assert.IsTrue(boundingRectangle.Contains("Height"));59 // Pattern specific attribute that may be used along with element.Selected property etc.60 Assert.AreEqual(element.GetAttribute("SelectionItem.IsSelected"), element.Selected.ToString());61 Assert.AreEqual(element.GetAttribute("IsSelectionItemPatternAvailable"), "True");62 Assert.AreEqual(element.GetAttribute("IsSelectionPatternAvailable"), "False");63 }64 [TestMethod]65 public void GetElementAttribute_InvalidAttribute()66 {67 // Getting the value of an invalid/unset attribute should return null68 string invalidAttribute = alarmTabElement.GetAttribute("InvalidAttribute");69 Assert.AreEqual(invalidAttribute, null);70 }71 [TestMethod]72 public void GetElementAttributeError_NoSuchWindow()73 {74 try75 {76 var attribute = Utility.GetOrphanedElement().GetAttribute("Attribute");77 Assert.Fail("Exception should have been thrown");78 }79 catch (InvalidOperationException exception)80 {81 Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);82 }83 }84 [TestMethod]85 public void GetElementAttributeError_StaleElement()86 {87 try88 {89 var attribute = GetStaleElement().GetAttribute("Attribute");90 Assert.Fail("Exception should have been thrown");91 }92 catch (InvalidOperationException exception)93 {94 Assert.AreEqual(ErrorStrings.StaleElementReference, exception.Message);95 }96 }97 }98}...
ElementAttribute
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using OpenQA.Selenium;7using OpenQA.Selenium.Firefox;8using OpenQA.Selenium.Support.UI;9using WebDriverAPI;10{11 {12 static void Main(string[] args)13 {14 IWebDriver driver = new FirefoxDriver();15 ElementAttribute elementAttribute = new ElementAttribute(driver);16 elementAttribute.GetAttribute("lst-ib", "name");17 elementAttribute.GetAttribute("lst-ib", "id");18 elementAttribute.GetAttribute("lst-ib", "type");19 elementAttribute.GetAttribute("lst-ib", "class");20 elementAttribute.GetAttribute("lst-ib", "value");21 elementAttribute.GetAttribute("lst-ib", "placeholder");22 elementAttribute.GetAttribute("lst-ib", "maxlength");23 elementAttribute.GetAttribute("lst-ib", "title");24 elementAttribute.GetAttribute("lst-ib", "dir");25 elementAttribute.GetAttribute("lst-ib", "lang");26 elementAttribute.GetAttribute("lst-ib", "spellcheck");27 elementAttribute.GetAttribute("lst-ib", "role");28 elementAttribute.GetAttribute("lst-ib", "aria-haspopup");29 elementAttribute.GetAttribute("lst-ib", "aria-autocomplete");30 elementAttribute.GetAttribute("lst-ib", "aria-activedescendant");31 elementAttribute.GetAttribute("lst-ib", "aria-expanded");32 elementAttribute.GetAttribute("lst-ib", "aria-owns");33 elementAttribute.GetAttribute("lst-ib", "aria-label");34 elementAttribute.GetAttribute("lst-ib", "aria-describedby");35 elementAttribute.GetAttribute("lst-ib", "aria-disabled");36 elementAttribute.GetAttribute("lst-ib", "aria-required");37 elementAttribute.GetAttribute("lst-ib", "aria-invalid");38 elementAttribute.GetAttribute("lst-ib", "aria-multiline");39 elementAttribute.GetAttribute("lst-ib", "aria-readonly");40 elementAttribute.GetAttribute("lst-ib", "aria-activedescendant");41 elementAttribute.GetAttribute("lst-ib", "aria-autocomplete");42 elementAttribute.GetAttribute("lst-ib", "aria-haspopup");43 elementAttribute.GetAttribute("lst-ib", "role");44 elementAttribute.GetAttribute("lst-ib
ElementAttribute
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Support.UI;3using System;4using System.Collections.Generic;5using System.Linq;6using System.Text;7using System.Threading.Tasks;8{9 {10 static void Main(string[] args)11 {12 IWebDriver driver = new ChromeDriver();13 driver.Manage().Window.Maximize();14 driver.FindElement(By.Name("q")).SendKeys("Selenium");15 driver.FindElement(By.Name("btnK")).Click();16 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));17 Console.WriteLine("Selenium is found");18 Console.ReadLine();19 driver.Quit();20 }21 }22}23using OpenQA.Selenium;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 driver.Manage().Window.Maximize();36 driver.FindElement(By.Name("q")).SendKeys("Selenium");37 driver.FindElement(By.Name("btnK")).Click();38 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));39 Console.WriteLine("Selenium is found");40 Console.ReadLine();41 driver.Quit();42 }43 }44}45using OpenQA.Selenium;46using OpenQA.Selenium.Support.UI;47using System;48using System.Collections.Generic;49using System.Linq;50using System.Text;51using System.Threading.Tasks;52{53 {54 static void Main(string[] args)55 {56 IWebDriver driver = new ChromeDriver();57 driver.Manage().Window.Maximize();58 driver.FindElement(By.Name("q")).SendKeys("Selenium");59 driver.FindElement(By.Name("btnK")).Click();60 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
ElementAttribute
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Chrome;3using System;4using System.Threading;5{6 {7 static void Main(string[] args)8 {9 IWebDriver driver = new ChromeDriver();10 driver.Manage().Window.Maximize();11 Thread.Sleep(2000);12 IWebElement search = driver.FindElement(By.Name("q"));13 Console.WriteLine("Attribute value of type is: " + search.GetAttribute("type"));14 Console.WriteLine("Attribute value of name is: " + search.GetAttribute("name"));15 Console.WriteLine("Attribute value of class is: " + search.GetAttribute("class"));16 Console.WriteLine("Attribute value of id is: " + search.GetAttribute("id"));17 Console.WriteLine("Attribute value of maxlength is: " + search.GetAttribute("maxlength"));18 Console.WriteLine("Attribute value of title is: " + search.GetAttribute("title"));19 Console.WriteLine("Attribute value of role is: " + search.GetAttribute("role"));20 Console.WriteLine("Attribute value of value is: " + search.GetAttribute("value"));21 Console.WriteLine("Attribute value of autocomplete is: " + search.GetAttribute("autocomplete"));22 Console.WriteLine("Attribute value of spellcheck is: " + search.GetAttribute("spellcheck"));23 Console.WriteLine("Attribute value of dir is: " + search.GetAttribute("dir"));24 Console.WriteLine("Attribute value of data-ved is: " + search.GetAttribute("data-ved"));25 Console.WriteLine("Attribute value of aria-autocomplete is: " + search.GetAttribute("aria-autocomplete"));26 Console.WriteLine("Attribute value of aria-haspopup is: " + search.GetAttribute("aria-haspopup"));27 Console.WriteLine("Attribute value of aria-label is: " + search.GetAttribute("aria-label"));28 Console.WriteLine("Attribute value of placeholder is: " + search.GetAttribute("placeholder"));29 Console.WriteLine("Attribute value of aria-expanded is: " + search.GetAttribute("aria-expanded"));30 Console.WriteLine("Attribute value of aria-owns is: " + search.GetAttribute("aria-owns"));31 Console.WriteLine("Attribute value of aria-activedescendant is: " + search.GetAttribute("aria-activedescendant"));32 Console.WriteLine("Attribute value of aria-controls is: " + search.GetAttribute("aria-controls"));33 Console.WriteLine("Attribute value of aria-label is: " + search.GetAttribute("aria-label
ElementAttribute
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Support.UI;3using OpenQA.Selenium.Firefox;4using System;5using System.Collections.Generic;6using System.Linq;7using System.Text;8using System.Threading.Tasks;9using System.Threading;10using System.Collections.ObjectModel;11{12 {13 static void Main(string[] args)14 {15 IWebDriver driver = new FirefoxDriver();16 ElementAttribute attribute = new ElementAttribute();17 attribute.SetAttribute(driver, "q", "name", "Selenium");18 string value = attribute.GetAttribute(driver, "q", "name");19 Console.WriteLine(value);20 driver.Close();21 }22 }23}24using OpenQA.Selenium;25using OpenQA.Selenium.Support.UI;26using OpenQA.Selenium.Firefox;27using System;28using System.Collections.Generic;29using System.Linq;30using System.Text;31using System.Threading.Tasks;32using System.Threading;33using System.Collections.ObjectModel;34{35 {36 public void SetAttribute(IWebDriver driver, string value, string attribute, string locator)37 {38 IWebElement element = driver.FindElement(By.Name(locator));39 ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].setAttribute(arguments[1], arguments[2])", element, attribute, value);40 }41 public string GetAttribute(IWebDriver driver, string attribute, string locator)42 {43 IWebElement element = driver.FindElement(By.Name(locator));44 string value = (string)((IJavaScriptExecutor)driver).ExecuteScript("return arguments[0].getAttribute('" + attribute + "');", element);45 return value;46 }47 }48}
ElementAttribute
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Chrome;3using System;4{5{6static void Main(string[] args)7{8IWebDriver driver = new ChromeDriver();9driver.FindElement(By.Name("q")).SendKeys("Selenium C#");10driver.FindElement(By.Name("btnG")).Click();11driver.Close();12}13}14}
ElementAttribute
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 driver.Manage().Window.Maximize();15 driver.FindElement(By.Name("q")).SendKeys("Selenium");16 driver.FindElement(By.Name("btnG")).Click();17 driver.FindElement(By.LinkText("Selenium - Web Browser Automation")).Click();18 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));19 wait.Until(ExpectedConditions.ElementIsVisible(By.Id("menu_download")));20 driver.FindElement(By.Id("menu_download")).Click();21 driver.FindElement(By.LinkText("3.141.59")).Click();22 driver.Quit();23 }24 }25}26using OpenQA.Selenium;27using OpenQA.Selenium.Chrome;28using OpenQA.Selenium.Support.UI;29using System;30using System.Collections.Generic;31using System.Linq;32using System.Text;33using System.Threading.Tasks;34{35 {36 static void Main(string[] args)37 {38 IWebDriver driver = new ChromeDriver();39 driver.Manage().Window.Maximize();40 driver.FindElement(By.Name("q")).SendKeys("Selenium");41 driver.FindElement(By.Name("btnG")).Click();42 driver.FindElement(By.LinkText("Selenium - Web Browser Automation")).Click();43 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));44 wait.Until(ExpectedConditions.ElementIsVisible(By.Id("menu_download")));45 driver.FindElement(By.Id("menu_download")).Click();46 driver.FindElement(By.LinkText("3.141.59")).Click();47 driver.Quit();48 }49 }50}51{52 {53 static void Main(string[] args)54 {55 IWebDriver driver = new ChromeDriver();56 driver.Manage().Window.Maximize();57 driver.FindElement(By.Name("q")).SendKeys("Selenium");58 driver.FindElement(By.Name("btnK")).Click();59 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
ElementAttribute
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Support.UI;3using OpenQA.Selenium.Firefox;4using System;5using System.Collections.Generic;6using System.Linq;7using System.Text;8using System.Threading.Tasks;9using System.Threading;10using System.Collections.ObjectModel;11{12 {13 static void Main(string[] args)14 {15 IWebDriver driver = new FirefoxDriver();16 ElementAttribute attribute = new ElementAttribute();17 attribute.SetAttribute(driver, "q", "name", "Selenium");18 string value = attribute.GetAttribute(driver, "q", "name");19 Console.WriteLine(value);20 driver.Close();21 }22 }23}24using OpenQA.Selenium;25using OpenQA.Selenium.Support.UI;26using OpenQA.Selenium.Firefox;27using System;28using System.Collections.Generic;29using System.Linq;30using System.Text;31using System.Threading.Tasks;32using System.Threading;33using System.Collections.ObjectModel;34{35 {36 public void SetAttribute(IWebDriver driver, string value, string attribute, string locator)37 {38 IWebElement element = driver.FindElement(By.Name(locator));39 ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].setAttribute(arguments[1], arguments[2])", element, attribute, value);40 }41 public string GetAttribute(IWebDriver driver, string attribute, string locator)42 {43 IWebElement element = driver.FindElement(By.Name(locator));44 string value = (string)((IJavaScriptExecutor)driver).ExecuteScript("return arguments[0].getAttribute('" + attribute + "');", element);45 return value;46 }47 }48}
ElementAttribute
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 driver.Manage().Window.Maximize();15 driver.FindElement(By.Name("q")).SendKeys("Selenium");16 driver.FindElement(By.Name("btnG")).Click();17 driver.FindElement(By.LinkText("Selenium - Web Browser Automation")).Click();18 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));19 wait.Until(ExpectedConditions.ElementIsVisible(By.Id("menu_download")));20 driver.FindElement(By.Id("menu_download")).Click();21 driver.FindElement(By.LinkText("3.141.59")).Click();22 driver.Quit();23 }24 }25}26using OpenQA.Selenium;27using OpenQA.Selenium.Chrome;28using OpenQA.Selenium.Support.UI;29using System;30using System.Collections.Generic;31using System.Linq;32using System.Text;33using System.Threading.Tasks;34{35 {36 static void Main(string[] args)37 {38 IWebDriver driver = new ChromeDriver();39 driver.Manage().Window.Maximize();40 driver.FindElement(By.Name("q")).SendKeys("Selenium");41 driver.FindElement(By.Name("btnG")).Click();42 driver.FindElement(By.LinkText("Selenium - Web Browser Automation")).Click();43 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));44 wait.Until(ExpectedConditions.ElementIsVisible(By.Id("menu_download")));45 driver.FindElement(By.Id("menu_download")).Click();46 driver.FindElement(By.LinkText("3.141.59")).Click();47 driver.Quit();48 }49 }50}
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!!