Selenium webdriver (c#) - Finding button based on attribute
Solution 1
use xpath for based on the attribute "gl-command"
driver.FindElement(By.XPath(“//*[@gl-command='transaction']”)).Click();
Solution 2
You can also create a custom selector with a method that returns your needed By selector for easy future use like this:
public static By SelectorByAttributeValue(string p_strAttributeName, string p_strAttributeValue)
{
return (By.XPath(String.Format("//*[@{0} = '{1}']",
p_strAttributeName,
p_strAttributeValue)));
}
And use it like this:
driver.FindElement(Selectors.SelectorByAttributeValue("data-power","5"))
Solution 3
XPath would be your safest bet.
IWebElement glButton = driver.findElement(By.xpath("//button[contains(@gl-command, 'transaction')]));
There's a similar question here: http://forum.testproject.io/index.php?topic=66.0
Solution 4
Not sure if you are asking to find a button IF it has the gl-command attribute or if the value of gl-command is some specific value so I'll answer both ways.
Find buttons with gl-command attribute
driver.FindElements(By.Tag("button")).Where(x => !string.IsNullOrEmpty(x.GetAttribute("gl-command")).FirstOrDefault();
Can remove the FirstOrDefault()
if you want all buttons with gl-command attribute.
Find buttons with specific gl-command value
driver.FindElements(By.Tag("button")).Where(x => !string.IsNullOrEmpty(x.GetAttribute("gl-command") && string.Compare(x.GetAttribute("gl-command"), "YOURGLCMD", StringComparison.OrdinalIgnoreCase) == 0));
My closing parens might be off because I typed all this out on my phone in bed, but that's the general gist and my gf is yelling at me to go to sleep.
Banjaxx
Updated on June 09, 2022Comments
-
Banjaxx almost 2 years
I'm trying to get a handle on the button below based on the attribute
gl-command
. I'm aware I can find the button using aCssselector
by locator but I don't want to do that in this case.I should point out that this is just one of many buttons within the AUT:
<google-componentbutton size="32"></google-componentbutton>
<div class="gl-component-buttons"><gl-component-buttons id="gl-component-button-set-bottom"> <google-componentbutton size="32"> <button class="google-componentbutton glmdl-button glmdl-js-button glmdl-js-ripple-effect google-image gl-transaction-image" style="height: 32px; widgl: 32px; background-size: 24px 24px; background-position: 4px 4px;" gl-tooltip-id="google_component_transaction" gl-tooltip="transaction" data-upgraded=",MaterialButton,MaterialRipple" gl-command="transaction"> <span class="glmdl-button__ripple-container"> <span class="glmdl-ripple"></span> </span> </button> </google-componentbutton>