ProgSky
ProgSky

Reputation: 2620

Selenium C# Webdriver How to detect if button is clicked

I am using selenium with IE to automate web testing. My webpage fills out a form and finally clicks a button. Code to click button is

driver.FindElement(By.CssSelector("td.ne-fieldvalues > input[type=\"button\"]")).Click();

8 out of 10 times, it Clicks but other time click command is never executed. Is there way I can check if button was indeed clicked ? something similar to checkbox

if (!driver.FindElement(By.CssSelector("input[type=\"checkbox\"]")).Selected)
                    driver.FindElement(By.CssSelector("input[type=\"checkbox\"]")).Click();

I tried .Displayed and .Enabled and both these properties are always true. Thanks for help.

Upvotes: 1

Views: 3656

Answers (1)

Zeeshan S.
Zeeshan S.

Reputation: 2091

To ensure that an action is performed on a click of a link or button, it is best to verify the resultant state of application. For eg. if I click on 'Log in' button after entering valid username and password, it will take me to my homepage, verify that homepage has loaded, else fail the click event. In case of invalid username/password, verify the warning message on login page itself.

To summarize, you have to validate the response to verify click event on any web element.

The least you can do is ensure that the element is clickable using ExpectedConditions.elementToBeClickable():

// In Java
WebDriverWait wait = new WebDriverWait(driver, timeOut);
wait.until(ExpectedConditions.elementToBeClickable(locator));

Upvotes: 2

Related Questions