2

I have this page where an angularjs modal-content popup, in there i fill up some fields and click save. After save is initiated, popup should dissapear an event should happen and so on.

My selenium test does all that perfectly except that when it clicks on the save button, popup dissapears but no event is triggered or saved so when i open up the window again everything is empty. I've tried stuff that i know with selenium and it still doesn't work. Can anyone help me out here?

This is the save button:

<button class="save-button" data-ng-click="onSettingsSave()" ng-hide="readOnlyMode || !canSave()">Save</button>

Stuff i've tried:

var saveButton = driver.FindElement(By.CssSelector("button.save-button"));
saveButton.Click();

var saveButton = driver.FindElement(By.XPath(saveXpath));
saveButton.SendKeys(Keys.Enter);

((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].focus();",saveButton);
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();",saveButton );
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
fimo
  • 21
  • 2
  • 1
    What kind of exception you are getting? – Saifur Jul 13 '15 at 12:39
  • There is no exceptions, everything acts like its working fine but nothing is saved when the click happens through selenium on Save button. Window is just closed without triggering the "onSettingsSave()" function hence nothing saves. – fimo Jul 13 '15 at 16:14
  • Were you ever able to solve this issue? I am having the same problem, using truly pure JS from the console the click events work correctly, but triggered by selenium the click happens, but events are not fired. – Jereme Jun 10 '22 at 14:32

3 Answers3

1

Try force clicking the element using pure JS:

driver.execute_script("arguments[0].click();", yourElement)

csaladenes
  • 1,100
  • 1
  • 11
  • 27
  • This is not pure javascript, it is still running through the selenium engine. And according to the OP and my own testing, does not solve this issue unfortunately. – Jereme Jun 10 '22 at 14:33
0

Like the OP I have tried everything I can think of to get Selenium to trigger client side javascript events. I've seen some posts across the web of people having partial success where it randomly works; in my case it never works.

Selenium does successfully trigger the browsers primary click action, be it checking a checkbox or pressing a button, but it does not trigger any attached client side javascript events.

Both the native element.Click() method in selenium, and the abstracted ExecuteScript with arguments method of clicking as suggested by @csaladenes have the same result.

The only solution I have found so far is to use pure JS through that same ExecuteScript method; basically avoid the overload with params selenium can embed.

driver.ExecuteScript("$('#base_element_id div input').click()");

In my case I am using the JQuery that is already on my page to make locating the element easier, but any form of truly pure JS should do the same thing.

EDIT: After some additional testing, it turns out that my "fix" really did nothing. However, performing the same click more than once did cause the client side events to fire.

In my case I am checking a checkbox, so I needed to perform the click 3 times to leave it in the correct state and still have the client side events run.

This is very odd, and definitely needs some more work to figure out where the issue is at that makes this necessary.

Edit 2: I think I have finally found a solution, and at least partial answer, that does not make me cringe.

It seems as though Selenium has an issue where sometimes it "loses" the focus of the browser. Considering how consistent and repeatable my issue is I don't think focus is the only problem in my case, however the solution works pretty well.

I was able to get the immediate parent of my checkbox, which was a div element, click that first to return focus to the page, then click the checkbox. After that sequence of events the client side events worked correctly.

Jereme
  • 630
  • 6
  • 18
0

You can't use $ as a shortcut for document.querySelector in a script like that.

driver.ExecuteScript("document.querySelector('#base_element_id div input').click()");

Also this probably won't trigger an onClick in react / angular

pguardiario
  • 53,827
  • 19
  • 119
  • 159