Search code examples
javascripthtmlgreasemonkey

Need help clicking an ```<a href="">``` element that doesn't have any identifiers


I'm trying to write a script that comments on a certain post then clicks on the "Favourite this post" button but this button doesn't have any sort of ID assigned to it how would I click this button?

I've tried JQuery but it didn't work.

I'm trying to use this on FurAffinity to automatically click an <a> element without any sort of identifiers. When i check the element with dev. options on google it says that the name exists, it's just set to blank so I cannot choose it with JQuery.

The snippet example of the code of the website. I would like to click the "+Add to Favourites" text using a separate button that appears elsewhere on the page by the use of Tampermonkey.

I don't know how I would find the element by using just the InnerHTML of it. The <a> tag has no name or id or class, just an href and some text inside.

<div class="alt1 actions aligncenter" style="margin-top: 8px;">
                
                                    <b><a href="/fav/33151503/?key=37dfe6a83f38f48790a6551921ddb13850e2c6a0">+Add to Favorites</a></b> |
                                <b><a href="//d.facdn.net/art/legacy2988/stories/1569252982/1569252982.legacy2988_i’m_not_sure_atm.txt">Download</a></b> |
                <b><a href="/full/33151503/">Full View</a></b>

                
                                    | <b><a href="/newpm/legacy2988/">Send note</a></b>
                
                
                <div>Submission &copy; 2019 Legacy2988</div>
            </div>


Solution

  • Based on your sample code, you want to find this element:

    <a href="/fav/33151503/?key=37dfe6a83f38f48790a6551921ddb13850e2c6a0">+Add to Favorites</a>
    

    Assuming there is only 1 such link and it always has /fav/ in its href

    document.querySelector('a[href*="/fav/"]').click();
    

    Above can be further fine-tuned, if necessary but that should give you an idea.