5

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 a Cssselector 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>

5 Answers 5

11

use xpath for based on the attribute "gl-command"

driver.FindElement(By.XPath("//*[@gl-command='transaction']")).Click();
Sign up to request clarification or add additional context in comments.

1 Comment

Just a note: From their documentation - "a search prefixed with // will search the entire document, not just the children of this current node. Use .// to limit your search to the children of this WebElement". Note the period before the double forward slashes.
7

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"))

Comments

2

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

1 Comment

Tks this was exactly what i was looking for +1
1

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.

1 Comment

@stackoverflow.com/users/2387630/dmitry-k What is the using directive for .Where? I get 'IWebElement does not contain definition...'
0

If one of the class is unique you can use className

driver.FindElement(By.ClassName("google-componentbutton"));
// or
driver.FindElement(By.ClassName("glmdl-button"));
// etc

If none of them unique you can use combination of all of them or some of them

driver.FindElement(By.CssSelector(".google-componentbutton.glmdl-button.glmdl-js-button.glmdl-js-ripple-effect.google-image.gl-transaction-image"));
// or
driver.FindElement(By.CssSelector(".google-componentbutton.glmdl-button"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.