-1

I need to use selenium to click on button but I'm Facing some problems

I try this code but show me error "Selenium.InvalidSelectorException: 'invalid selector"

IList link = driver.FindElements(By.ClassName("button postfix"));

        foreach (IWebElement elem in link)
        {
            if (elem.GetAttribute("ng-click").Equals("quickSearch.search()"))
                elem.Click();
        }

html page code

<a href="javascript: void(0);" class="button postfix" ng-click="quickSearch.search()" analytics-on="click" analytics-event="InventoryManagementSearchKeyword" sc-omniture-props="InventoryManagementAllSS"><i class="fi-magnifying-glass"></i></a>

I try to use id but there's no Id for the button so I don't know how to use it

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Waleed
  • 47
  • 3
  • 2
    That exception message doesn't go with this code. If you had run the code posted you would have received an error like, "Compound class names not permitted". This is because you are feeding two class names into `By.ClassName()`. One way to fix that is to switch to a CSS selector like `By.CssSelector(".button.postfix")`. – JeffC Apr 22 '19 at 19:16
  • 2
    Having said that, you can improve the locator to include the `ng-click` attribute like the CSS selector `a.button.postfix[ng-click='quickSearch.search()']`. Now you don't have to loop. – JeffC Apr 22 '19 at 19:17
  • 1
    You should update your question with the current code you are running along with posting the exact (and full) error/exception message in the question. – JeffC Apr 22 '19 at 19:17

2 Answers2

0

You can use Xpath.

 driver.FindElement(By.XPath("//a[@class='button postfix' and @ng-click='quickSearch.search()']")).Click();
Abhay
  • 174
  • 1
  • 7
0

As the element is an Angular element so to invoke click() on the desired element you have to induce WebDriverWait for the ElementToBeClickable and you can use either of the following Locator Strategies:

  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("a.button.postfix[ng-click^='quickSearch'][analytics-event='InventoryManagementSearchKeyword']"))).Click();
    
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@class='button postfix' and starts-with(@ng-click, 'quickSearch')][@analytics-event='InventoryManagementSearchKeyword']"))).Click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352