Louis
Louis

Reputation: 3

clicking a button in javascript without button id

I am attempting to automate a button click on a website. The only problem is, the HTML for the button looks like this (please note that this is not the exact button I am trying to click, it is just a real example of what a lot of buttons look like):

<button type="submit" class="Ghost-btn">Continue</button>  

Obviously, there is no ID for this button, so I don't know how to 'click' on it. Is there a way of using the class name, or is there a way to program javascript to edit the HTML and add an ID for the button?

Thanks for your help in advance.

Louis

PS the link for the above button is https://raffle.sneakersnstuff.com/adidas-yeezy-boost-700-salt-EG7487 , and it is the first button you will see, labelled 'continue'.

Upvotes: 0

Views: 2457

Answers (3)

Avinash
Avinash

Reputation: 17

ID is just a handy and simple way for identifying any DOM element. You can use other attribute or a set of attributes unique to the document for this purpose.

For example "type" in your example, if this is the only input of type "submit" Or if there are more than one "submit" buttons, you can use type="submit" and class="Ghost-btn" or even text of the button.

Upvotes: 0

Syed Mehtab Hassan
Syed Mehtab Hassan

Reputation: 1337

You can use

  • document.getElementsByClassName("Ghost-btn");
  • document.getElementsByTagName("BUTTTON");

but be sure that there might be more than one components so have to select the right one.

Upvotes: 0

toastrackengima
toastrackengima

Reputation: 8742

You can use document.querySelector to use any CSS selector to select an element in JS, for example:

document.querySelector(".Ghost-btn").click();

Read more about this at MDN.


This would also allow you to select the button in other ways, for instance:

document.querySelector("button[type=\"submit\"]").click();

is also valid.

Upvotes: 2

Related Questions