Trying to open webpage and auto click some button

Hi I trying to open webpage and auto to click some button
but it will not working in my iPhone , please tell me how can i fix that

let url = "https://shopee.tw/shopee-coins";

let wv = new WebView()
await wv.loadURL(url)
//get elements with mobile mode
let js =`document.getElementsByClassName('.mobilemall-coinsrewardpage_327hN9').click();`

wv.evaluateJavaScript(js)
await wv.present()

What is guaranteed to have happened when loadURL() completes? Therein might be your problem.

I think it should load the webpage then use js variable to click the button
but in loadURL() will not show it
so I change to after loadURL() should present()
but i don’t know is correct or not …

Two things:

  1. Your in-page JS is trying to click on an array of page elements, which doesn’t work, because the click() method can only act on a single element. You’ll need to use document.getElementsByClassName(...)[0] to isolate the element you want to click.
  2. I suggest awaiting wv.evaluateJavaScript(js) so that it has already finished running by the time you see it.

So like this:

let url = "https://shopee.tw/shopee-coins";

let wv = new WebView()
await wv.loadURL(url)
//get elements with mobile mode
let js =`document.getElementsByClassName('.mobilemall-coinsrewardpage_327hN9')[0].click();`

await wv.evaluateJavaScript(js)
await wv.present()
1 Like

thanks , suggest
but that element is single element
so use document.getElementsByClassName(...)[0] will get error

It shouldn’t though. Even though it only grabs one element, it’s still an array–with one item in it. That’s how the getElementsByClassName() method works. It’s not like getElementById which returns a lone element.

try document.querySelector('.mobilemall-coinsrewardpage_327hN9') which should give you the first intance of the element.

I try it , but when first time to open webview then will not click the element
after close webview and run agin the element will clicked

Try wrapping your DOM code in DOMContentLoaded first, basically making sure that the element your trying to click already is loaded.

window.addEventListener('DOMContentLoaded', () => {
    document.querySelector('.mobilemall-coinsrewardpage_327hN9').click();
});