0

Is it possible to load a page in the background and click on a button of the loaded page on JavaScript?

I want to create an auto checkout script. I want to load the next page from the checkout process and get the button "buy" clicked which takes me to the payment page. How can I achieve this?

Looky1173
  • 440
  • 6
  • 17
Mahal
  • 7
  • 2
  • 1
    Are you trying to programmatically click a button? If so, assuming you have access to the DOM, you can do that like this: `document.getElementById('myButton').click()`. You can read more about the click method [here](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click). By the way, handling sensitive actions like clicking a "Pay now" button should be done by the user; otherwise, your site might gain a shady reputation. – Looky1173 Aug 27 '21 at 19:33
  • 'document.getElementById('myButton').click()' works only if im currently on the site where the button is. Lets assume im on the frontpage, i want to execute a script where i click a button of a subpage – Mahal Aug 27 '21 at 19:58
  • I think i found a solution: https://stackoverflow.com/questions/824349/how-do-i-modify-the-url-without-reloading-the-page – Mahal Aug 27 '21 at 20:02
  • So basically you want to redirect the user from the home page to a subpage then click a button for them on that page? Or are you trying to navigate your website without reloading? – Looky1173 Aug 27 '21 at 20:02
  • yes i want to navigate and use buttons without reloading the page. Would be the stuff with pushState the right thing? – Mahal Aug 27 '21 at 20:24
  • 1
    Yes, [`pushState`](https://developer.mozilla.org/en-US/docs/Web/API/History/pushState) is the way to go. However, it only changes the URL and doesn't manipulate the DOM so you have to take care of that separately. I recommend you search for "JavaScript SPA (Single Page Application) tutorial" to get started. – Looky1173 Aug 27 '21 at 20:46
  • @looky1173 for what does DOM stand for and thank you (edit found it: Document Object Model) – Mahal Aug 27 '21 at 20:48
  • 2
    DOM stands for Document Object Model and is the data representation of the objects that comprise a webpage. I suggest you read [this](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction) to get more familiar with the DOM. :-) – Looky1173 Aug 27 '21 at 20:53
  • Please provide enough code so others can better understand or reproduce the problem. – Community Aug 28 '21 at 16:04

0 Answers0