1

I am testing, and I have to click on elements, but it sometimes click, sometimes not. I tried some solutions, but there is no one, which always clicks. Is there any solution to click an element, which always works?

I tried these codes:

1:

await driver.findElement(By.id("u_0_a")).click();

2:

var element = await driver.findElement(By.id("u_0_a"));
await element.click();

3:

await driver.executeScript("document.getElementById('u_0_a').click()");

I use selenium with node.js, javascript, chrome driver.

2
  • The click() function in JavaScript does not execute any "click" event handlers. Commented Jun 18, 2019 at 11:57
  • Did you ever figure this out? Commented Apr 8, 2021 at 6:58

4 Answers 4

2

Try using:

var element = await driver.findElement(By.id("u_0_a"));
await driver.executeScript("arguments[0].click();", element)

Hope this helps!

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

1 Comment

Thank you for help, but it is the same :( Sometimes click, sometimes not. :(
0

Try using TryUntil. I hope this works. The below code is in C#

Browser.TryUntil(() => { }, () => Browser.FindElement(By.id("u_0_a")).Exists(), TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(120));
Browser.FindElement(By.id("u_0_a")).Click();          

1 Comment

Well it is C#, but I try to write it in JS.
0

Thank you for your help. The problem was not in my code. The problem was with the https://www.facebook.com/ . Because facebook, is changing the ID, and sometimes it was the login button sometimes a gender custom button. :/

Comments

-1

Please try sleep before you handle the click operation. Example as below:

await driver.sleep(2000)
await driver.findElement(By.xpath('element path')).click()

1 Comment

generally using sleep should be considered the final option. Incorporating sleeps into your test will slowly make your test extremely sleep and very difficult to run.

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.