0

Update:

After further investigation, actually what's happening is when the checkbox is checked using jquery, the next step is to click the button to save. Well, for some unknown reason, the checkbox is unchecking itself.

Here's the jquery to select the checkbox:

            string jsStmt = @"
$(function(){

$(':checkbox').filter(function(){
    return $(this).parent().next('td').text() === 'Label to look for';
}).prop('checked', true);

});
";


            IJavaScriptExecutor js = (IJavaScriptExecutor)_driver;
            js.ExecuteScript(jsStmt);

I can see the checkbox get checked, then when the dialog closes I can see it get unchecked?

OP:
Button click event for Selenium testing has been working for a while. All of a sudden, it's not working anymore. Only possible change was some 3rd party css change but I don't know what it could've been.

The button is kind of buried in layers, however, there's a checkbox that gets checked by Selenium that is still working fine, and it's the reason why the button needs to be clicked.

I can debug, and see that Selenium is finding the button. It appears that the button gets clicked but the postback event doesn't occur.

I can stop execution right before the click event and open developer tools and manually type in the statement to click the button, AND that works!

I even tried executing the click with JavaScriptExecutor with no luck.

I am so lost why it's Not firing postback event.

7
  • Which driver are you using? Commented Mar 1, 2017 at 15:07
  • 1
    Please post the code you are using along with any error messages, etc. Also post the relevant HTML or preferably a link to the page. Commented Mar 1, 2017 at 15:20
  • I am using IEDriverServer.exe 2.45.0.0 and I've tried on latest 3.1.x.x and doesn't work there either. Commented Mar 1, 2017 at 15:25
  • 1
    @Rod if you're using selenium my guess is you want the test to behave like a user. Why not use selenium to check the box instead of jquery? IMO using the IJavaScriptExecutor should be extrememly rare or not used at all in selenium tests. Using selenium will more accurately reflect a user clicking on the checkbox, where using javascript might allow you to do actions that a user wouldn't be able to do, making you think everything is fine when the test passes. Commented Mar 1, 2017 at 22:54
  • @Rod If you can provide some html for the checkbox and submit button, I'd be glad to help with some selenium code to click it. Commented Mar 1, 2017 at 22:55

1 Answer 1

1

In short, try clicking the checkbox using an IWebElement instead of javascript.

I would suggest avoiding using IJavaScriptExecutor as much as possible in selenium tests as it can lead to performing functions the user wouldn't actually be able to do with simple interactions, or having those functions behave differently than you would expect because the execution path is different than what a user would do if they just clicked something.

I think that's what you're running to in this case where you're trying to mark the box as checked.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.