0
<button class="btn btn-outline-primary btn-lg" id="btnPermission">GO</button>

The above codes are from a website, however I am unable to click this button despite trying multiple attempts. What should I do?

 IWebElement reklamgec3 = driver.FindElement(By.CssSelector("#btnPermission"));
 ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", reklamgec3);



IWebElement reklamgec3 = driver.FindElement(By.ClassName("btn-outline-primary"));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", reklamgec3)


IWebElement reklamgec3 = driver.FindElement(By.XPath("//*[@id="btnPermission"]"));
   ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", reklamgec3)


 IWebElement reklamgec3 = driver.FindElement(By.Id("btnPermission"));
  ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", reklamgec3)

      IWebElement reklamgec3 = driver.FindElement(By.CssSelector("#btnPermission"));
        reklamgec3.Click();

How to click button using c# selenium. The above does not work for me.

  • Can't you call [`Click()`](https://www.selenium.dev/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_IWebElement_Click.htm) directly on `reklamgec3`? – Thomas Weller Feb 12 '21 at 12:16
  • I tried but it doesn't work – welomofnorm Feb 12 '21 at 12:17
  • Maybe you want to [edit] the question and list all options you tried. Otherwise you'll get a lot of suggestions that are not useful. – Thomas Weller Feb 12 '21 at 12:19
  • I added those that don't work for me – welomofnorm Feb 12 '21 at 12:28
  • I think @ThomasWeller is saying `reklamgec3.Click()` since the Click() method is a method on the IWebElement interface. Have you put a breakpoint on to see if `reklamgec3` actually has a value? – squillman Feb 12 '21 at 12:29
  • No. I didn't put it because i don't know how. – welomofnorm Feb 12 '21 at 12:34
  • "It does not work" is a vague description. Does it throw an exception? If so, what exception? – Thomas Weller Feb 12 '21 at 12:38
  • I'm getting a no such element unable to locate element error. – welomofnorm Feb 12 '21 at 12:40
  • I agree to @squillman: you certainly should know how to put a breakpoint. Maybe watch this: https://youtu.be/KZ7J4VryLE8?t=98 – Thomas Weller Feb 12 '21 at 12:44
  • 1
    If there is no such element, but you see it when you have a look at the website manually, chances are that the element is not available at page load but is added later by JavaScript. You then need something like [Wait](https://stackoverflow.com/questions/47251939/wait-until-button-is-clicked-in-selenium-webdriver-to-click-on-next-button) or [Wait](https://stackoverflow.com/questions/6992993/selenium-c-sharp-webdriver-wait-until-element-is-present) – Thomas Weller Feb 12 '21 at 12:44
  • What should I do in this situation? @ThomasWeller – welomofnorm Feb 12 '21 at 12:47

1 Answers1

0

You need to use an explicit wait:

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));

wait.Until(d => d.FindElement(By.Id("btnPermission")).Click());

Waiting for the existence of a element is frequently not enough. You need to wait for it to be clickable. Many times that just means attempting to find then click the element until the click event succeeds.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92