0

I have a button class that is placed inside an iframe. Problem is that this button's class has such a long name and I do not understand how to reference it in my script.

<button class="PDF-dmzpd5z6ckdkxkn8 PDF-5rbqp8nfgh6e11 PDF-tma5quj Toolbar-Button Tool-Button" title="SignUp" aria-label="myButton001" type="button"></button>

I am using my javascript to reference this class as:

document.querySelector("iframe").contentWindow.document.querySelector(".PDF-dmzpd5z6ckdkxkn8").click();

The above code does not work. Do I have to provide the complete class name for reference?

I am on right track because I have another button that looks like this:

<button class="PDF-tdsfethgr51stg Next-Button Next-Previous-Button" title="Next" aria-label="Next" type="button"></button>

And I can easily call/reference it via:

document.querySelector("iframe").contentWindow.document.querySelector(".PDF-tdsfethgr51stg").click();
10
  • Those buttons have no id at all. Commented Feb 7, 2022 at 11:19
  • @tevemadar sorry I meant class name Commented Feb 7, 2022 at 11:22
  • 3
    Are you sure the content is already loaded when the script is executed? The iframe might have dynamic elements being added asynchronously. Commented Feb 7, 2022 at 11:26
  • 1
    Those look like class-names generated by React or some other runtime framework. If you're bypassing that framework and manipulating the DOM directly then you'll likely run into issues. Please tell us where your script is running and if React.js or some other framework (Vue.js?) is being used. Commented Feb 7, 2022 at 11:28
  • 1
    What errors/output do you see in your browser console? Commented Feb 7, 2022 at 11:28

2 Answers 2

3

If selecting with class doesn't work, you can try to select with title attribute:

document.querySelector('button[title="SignUp"]');
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. That actually worked.
0

Using Id in button is the best option

Example:

<button class="PDF-dmzpd5z6ckdkxkn8 PDF-5rbqp8nfgh6e11 PDF-tma5quj Toolbar-Button Tool-Button" title="SignUp" aria-label="myButton001" type="button" id="exbtn1"></button>
document.querySelector("iframe").contentWindow.document.querySelector("#exbtn1").click();

Note: It's best option to define the tags in Query Selector (eg. document.querySelector("iframe")[0]). The index starts from 0.

1 Comment

Thank you but I have no control over the HTML elements. I am using javascript injection to trigger it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.