I’m new to Selenium and C#.
I know this code is not optimal, can you advise how I can write such things quicker and shorter?
Basically I am searching for a button, href of which contains “addNewProduct”.
var addButtons = _driver.FindElements(By.LinkText("Add"));
IWebElement addNewProductButton = null;
foreach (IWebElement button in addButtons) {
if (button.GetAttribute("href").Contains("addNewProduct")){
addNewProductButton = button;
break;
}
}
addNewProductButton.Click();
Make use of XPath or CSS Selector.
XPath
CSS Selector
I recommend CSS Selector since they are faster and the syntax is more concise.