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

后端 未结 5 1724
渐次进展
渐次进展 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 12:03

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

提交回复
热议问题