0

How to extract CSS selector of a button when clicked on it?

I'm trying to build a plugin where I need the CSS selector of a button when clicked on it so that I can automate the clicking process when we visit the URL again using document.querySelector(). So how to extract the CSS selector as a string?

After inspecting and copying the selector of a button-

manually copying the css selector

I get this value - #submit_identifier > span.ladda-label. How to get the same value using JavaScript when I click on the button?

srini
  • 11
  • 1
  • 1
    Have you made an effort to write this yourself, how far did you get? We're here to help with your code problems but we won't write your code *for* you. Please, post the (relevant) "*[mcve]*" code (HTML, CSS and JavaScript) and visit the "*[ask]*" page to see how to ask a good question. – David Thomas Dec 05 '22 at 10:33
  • Hi @DavidThomas, thanks for the suggestions. I'm not expecting any code, just trying to get the string which I can pass in document.querySelector() and I could not find any way to get that. – srini Dec 05 '22 at 11:05
  • Should we assume that you can't define an ID for this element (you don't have write access to the source code). And this plugin is for what library? – notrev Dec 05 '22 at 11:07
  • This plugin is for all URLs and none of the elements of any webpage are under my control. So I want to automate the clicking process by extracting the CSS selectors and saving them and when we visit the same webpage again. (for example after autofilling the input fields of a login page, I can automate the login button click using the selector.) – srini Dec 05 '22 at 11:18
  • Hi, how do you define *the* selector? You can go back through the ancestors, remembering all the types of element and their classes. Is that what you want to do? I can’t think of anything else as there is no other concept of *the* selector. – A Haworth Dec 05 '22 at 11:30
  • What type of plugin do you want? Is it a Greasemonkey/Tampermonkey style script, or a JavaScript module/script that you can add to, and use, on arbitrary sites? – David Thomas Dec 05 '22 at 11:43
  • take a look at this thread: [https://stackoverflow.com/questions/5728558/get-the-dom-path-of-the-clicked-a](https://stackoverflow.com/questions/5728558/get-the-dom-path-of-the-clicked-a) – Robbi Dec 05 '22 at 14:39

0 Answers0