0

On the UI I have a checkAll button which is implemented as a span under a div block. clicking it become hard so I used JavaScriptExcutor. I want click it two times. the code is as following 6 lines. the line3 always succeeded, but line 6 always fail silently (no error or exception, but click not happening). if I comment out line 3 then line 6 will function. why? please help me.

IJavaScriptExecutor executor = (IJavaScriptExecutor)drv;
IWebElement chkAll = TaskerStatus.FindElement(By.Id("header3-column0"));
executor.ExecuteScript("arguments[0].click();", chkAll);

cmdBar1collapseAll.Click();
expandAllBtn.Click();
executor.ExecuteScript("arguments[0].click();", chkAll);
5
  • I duplicate a line of line 6 (as line 7) then it worked. don't understand why line 6 is now working but has to be there to make line seven work. Commented Nov 30, 2018 at 19:06
  • correct a mistake in above comment: line 6 is now= line 6 is not Commented Nov 30, 2018 at 19:29
  • What if you target the element in the js: document.getElementById('header3-column0').click() Commented Dec 1, 2018 at 0:07
  • Hi pguardiario, I tried your suggestion. nothing changes. Thanks anyway. I think I figured out the root cause. Commented Dec 1, 2018 at 19:30
  • Ok good. You should answer your own question in that case. Commented Dec 1, 2018 at 22:19

1 Answer 1

2

If I have understood the usecase / issue properly, Line 6 i.e.:

executor.ExecuteScript("arguments[0].click();", chkAll);

fails silently, i.e. no error or exception and the click() not happening as well.

But again if you comment out line 3 i.e. the first instance of:

executor.ExecuteScript("arguments[0].click();", chkAll);

Then line 6 i.e. the second instance of:

executor.ExecuteScript("arguments[0].click();", chkAll);

functions well.

The reason Line 6 is failing because when you invoked click() on the IWebElement chkAll a JavaScript / jQuery might have been active.

Solution

You need to induce WebDriverWait for the IWebElement chkAll to be clickable before you invoke the click at Line 6 and you can update Line 6 as follows:

executor.ExecuteScript("arguments[0].click();", new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(TaskerStatus.FindElement(By.Id("header3-column0")))));
Sign up to request clarification or add additional context in comments.

1 Comment

Hi DebanijanB, Thank you for your suggestion. I tried your code. it throw a Timeoutexception. it also said the function of ExpectedContidtion is obsoleted. but I know what you mean. the timing sometimes is a problem. I figured out the root cause now. it is because the line4 code. This internal implementation caused the human eye didn't see the button click effect. but it actually did. I have a workaround to resolve it. but Thank you.

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.