Skip to content
CodeKruPro

Click a button through JavaScript

Automation taskMediumAsked in: Infosys, TCS, Wipro

"A normal click fails because another element covers the button. What do you do?" A JavaScript click works, but it skips the checks a real user goes through (visible, enabled, not covered). Be ready to explain that trade-off and say when you would accept it. Here you click the left-click button through JavaScript and print its new text.

Click Using JavaScript

Click Using JavaScript

Sometimes Selenium's click() fails due to overlapping elements. JavaScript click bypasses these issues.

Task:

  1. Navigate to https://testkru.com/Elements/Buttons
  2. Find the leftClick button
  3. Click it using JavaScript instead of Selenium's click()
  4. Print the button text after the JS click

Expected output:

After JS click: I was left-clicked!