Home > Software engineering >  How can I get a webpage to click on a button upon loading? Tried this and doesn't work
How can I get a webpage to click on a button upon loading? Tried this and doesn't work

Time:11-03

Would appreciate your help with this. I'm trying to get the site to automatically click on a button, when the site is loading.

The button I'm referring to is the blue one, above the fold right when you enter the page, the text is "Full 2022-2023 Schedule".

Site is running Elementor on Wordpress.

Any ideas?

I tried adding this script to the homepage of the site www.thenflschedule.com, but it just doesn't work.

<script type="text/javascript">
document.getElementById("button").click();
</script>

Update: There's a script on the page to open popups only when the window or any element is clicked. I thought that by auto-clicking a button, it would trigger the popup, but it doesn't. I would still need to click manually anywhere on the page for the pop up to open.

Is there a function to click anywhere on the window, that would trigger the popup script?

This is the pop up script that's running on the page: <script data-cfasync="false" src="//acacdn.com/script/suv4.js" data-adel="lwsu" cdnd="acacdn.com" zid="323555"></script>

CodePudding user response:

The button doesn't have an ID called "button". You can only use .getElementById when the element you are trying to access has an id parameter.
Try using

let x = document.getElementsByClassName("elementor-button-link elementor-button elementor-size-sm");
x[0].click();

That button (actually its a link tag) has the above classes thats why I have used .getElementsByClassName instead of .getElementById

CodePudding user response:

Thank you Viraj, looks like it's working now, but it doesn't do what I thought it would.

There's a script on the page to open popups only when the window or any element is clicked. I thought that by auto-clicking a button, it would trigger the popup, but it doesn't. I would still need to click manually anywhere on the page for the pop up to open.

Is there a function to click anywhere on the window, that would trigger the popup script?

This is the pop up script that's running on the page: <script data-cfasync="false" src="//acacdn.com/script/suv4.js" data-adel="lwsu" cdnd="acacdn.com" zid="323555"></script>

  • Related