I need to get the button to connect the Metamask wallet to Metis network (https://decentralize.metis.io/) using Javascript.
I want to get the Metamask button in below screen:
But the button is hidden hin shadow root of the page
I tried to use below script to access the Metamask button but I cannot
document.querySelector('w3m-modal').shadowRoot.querySelector('wui-flex style').shadowRoot.querySelector('wui-card').shadowRoot.querySelector('w3m-router').shadowRoot.querySelector('w3m-connect-view').shadowRoot.querySelector('wui-flex').shadowRoot.querySelectorAll('wui-list-wallet')[1].shadowRoot.querySelector('button').click();
But it cannot run and return the error. Could you please help to review and advise? Thank you.
2
Answers
Your button is placed inside several
shadow-roots
that are not rendered at the same moment after click on connect button.You should wait for every internal
shadow-root
to be rendered and only then check it’s structure and get another Shadow DOM.You can implement function
waitFor
that waits for function value to be truthy and return it’s value and functiondrillIntoShadowRoots
that loops through shadow host and wait for it’s internal structure to be rendered.After last structure is rendered, you get target element.
Modal shadow-hosts have tags correspondingly
'w3m-modal', 'w3m-router', 'w3m-connect-view', 'wui-list-wallet'
Try the below to click on the Metamask