enter image description here

I need to click on the above button just using


. I have tried with ;

> IWebElement el4m =
> Driver.FindElement(By.CssSelector("input[type='button'][contain='Search']"));
> IWebElement el4m =
> Driver.FindElement(By.CssSelector("input[type='button'][innerText='Search']"));
> IWebElement el4m =
> Driver.FindElement(By.CssSelector("input[type='button'][textContent='Search']"));

But nothing works fine. Can some 1 please advice me a correct CSSSelector for above button.

  • How can i know who make this as "-1" post :D – ChathuD Dec 5 '19 at 9:09

Selecting by visible text is only possible via xpath. That being said you can use the class selectors provided above, or create a more specific one, using the onclick parameter, i.e. :

By.CssSelector("button[onclick='new UserManagement().LoadExcelExportSearchResult();']")

If you want to stick with text, you need to capture all button elements and iterate a list of them, then select the one you want via Webelement.GetText().Contains() method.

| improve this answer | |
  • This works so fine @Moro – ChathuD Dec 9 '19 at 4:24

Hi css selector does not support using contained text as a identifier :


you could use xpath to find element using text

In your case , all your css locators are incorrect:

  1. the tag you are using is button and not input, so it should start with input
  2. There is no attribute called "type" in your given element

Valid format is :

The valid format is tag[attribute="value"]

so in this case it would be

  1. button[class="buttondefault"]
  2. button[class="buttondefault"][onclick="blabla"]
  3. button.buttondefault[onclick="blabla"] ( you can find element by class using tag.classvalue instead of tag[class="classvalue"])
| improve this answer | |
  • Thanks,This explain me lot. – ChathuD Dec 9 '19 at 4:01
  • Driver.FindElement(By.CssSelector("button[class*=buttondefault]")); using wildcard selector (*) which means if the class attribute has this proprety at any place it will find this element or
  • Driver.FindElement(By.CssSelector("button[class^=buttondefault]")); using caveat selector to find elements starting with "buttondefault" attribute or
  • Driver.FindElement(By.CssSelector("button[class=buttondefault]")); using = to find elements having class attribute equal to buttondefault
| improve this answer | |
  • This works fine. Thanks for the idea @Prome – ChathuD Dec 9 '19 at 3:59
  • I just edited the answer from @Astaqc Consulting . – Prome Dec 10 '19 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.