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:
Example 2
from this page:
Translate all reviews to English
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.
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()
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.
