I am trying - and failing - to write JavaScript to click this button:
The HTML for the button varies depending on the page
Example 1
from this page:
<span class="a-button-text" aria-hidden="true" id="a-autoid-8-announce">Translate all reviews to English</span> Example 2
from this page:
<a href="#customerReviews" class="a-button-text">Translate all reviews to English</a> Example 3
From this page:
<span class="a-button-text" aria-hidden="true" id="a-autoid-24-announce">Translate all reviews to English</span> Using the ID tag when it's available gets the desired result
document.querySelector("#a-autoid-8-announce").click() This works but only on pages where there's a dedicated ID tag—and even when there is one, it can vary. (Sometimes it's #a-autoid-8-announce and sometimes it's a-autoid-24-announce etc.)
Therefore, I want to select using a different method
The only things that are constant are:
- the anchor text
Translate all reviews to English - the class
a-button-text
Me trying to formulate an answer
So, the selectors should be:
a:contains('Translate').a-button-text
What I am stuck on
The code I need will (I assume) ultimately look like this:
document.querySelector(1 2).click()
edit: clarification: I mean document.querySelector("a:contains('Translate').a-button-text").click()
I have tried multiple permutations and combinations of the above, and none seem to work. 😩
I have tried, for instance, 2 1 (as well as 1 2), and I have tried putting 1 and 2 in single quotes, double quotes, and no quotes. (Every time I make a small change I hope for it to work, and it doesn't and a small part of me inside dies.)
I am now wondering if I need to write a loop, and this is giving me anxiety since I don't understand them (but I want to learn). Perhaps I don't need to do a loop, as I only need to click one link (if it's there on the page), but perhaps I still need to do it that way?
I am embarrassed asking this, as it's very remedial. I usually get the answer in the end through trial and error and reading guides. But this time the answer eludes me.

document.querySelector("1.2").click()would select a 1 tag with a class of 2, such as<1 class="2></1>. This doesn't exist in your code, nor is<1>a valid html tag. What are you expecting this to do and what do 1 and 2 mean in this instance?data-attribute?Translate all reviews to English)