0

enter image description here

I need to click on the above button just using

CSSSelector

. 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.

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

3 Answers 3

1

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.

1
  • This works so fine @Moro
    – ChathuD
    Dec 9, 2019 at 4:24
1

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

https://www.w3.org/TR/selectors-3/#content-selectors

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"])
1
  • Thanks,This explain me lot.
    – ChathuD
    Dec 9, 2019 at 4:01
1
  • 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
2
  • This works fine. Thanks for the idea @Prome
    – ChathuD
    Dec 9, 2019 at 3:59
  • I just edited the answer from @Astaqc Consulting .
    – Prome
    Dec 10, 2019 at 8:12

Your Answer

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

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