WebView exercise

Hello everybody, I’m new to the forum.

I’m grappling with a problem without finding the solution, so I ask you for help.

I want to load a page, then enter my username and password, then press the “log in” button, and then retrieve a data table.

For the first part I managed without problems:
var v = new WebView();
await v.loadURL(“https://www.youbike.cloud/login”);

var Js = document.getElementById('username').value = 'MYUSERNAME'; document.getElementById('password').value = 'MYPASSWORD'; document.getElementById('_submit').click();
await v.evaluateJavaScript(Js);
await v.present();

This script works fine, autologin works so when I present the WebView I see the second page of youbike site where there is a table under a div element with id ‘mappa’ that I want to retrieve.

BUT if I comment await v.present(); and add this other portion of the script:

var Js = console.log(document.getElementById('mappa'));
await v.evaluateJavaScript(Js);

I see in log. This is because after Scriptable automatically clicks button id “_submit”, the second page displays after a while, so I need something to wait that this second page is completely loaded, or to wait that document.getElementByid(“mappa”) is not null before I can access it.

I tried await v.waitForLoad(); without success.

How can I achieve this result?

Thank you in advance for your help.

Did you ever get an answer to your question? I am able to submit the form and see via getHTML() that the form is submitted and the resulting page is loaded.

However I try to evaluate JavaScript on the subsequent page it doesn’t work. Not sure if you can call evaluateJavascript() twice.

I will likely just process the html to extract out the data I need.

@paracytopenia evaluateJavascript() should work multiple times. At least it does for me.

@Alessio_Di_Giorgio If it is still relevant to you, you were on the right path with waitForLoad(). Maybe you just didn’t call it in the right order… And note that it can take some time, even when the page seems to have finished loading (when presenting the WebView simultaneously), because an image is still loading.

It should be:

let viewPromise = v.present();
let js = "<js script for login>";
await v.evaluateJavascript(js);
await v.waitForLoad();  // that can take some time, e.g. until the last image has loaded too
js = "<js script to retrieve table data>"
let result = await v.evaluateJavascript(js);
// do whatever with the data

// end of script, wait until the WebView is closed
await viewPromise;

I once made an auto login script to open the tracking page of AliExpress, where it automatically selected parcel 2 and therefore had to wait for the completed login. It worked flawlessly, only waiting a bit longer than I would to click the button for parcel 2.