Selenium webdriver (c#) - Finding button based on attribute

后端 未结 5 1694
渐次进展
渐次进展 2021-01-13 11:22

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

5条回答
  •  终归单人心
    2021-01-13 11:50

    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.

提交回复
热议问题