Login to website and download info

Hi everyone

I know this question has been asked on here as I’ve searched and gone through the code but I am having difficulties. I’m not sure if it’s due to software changing or if it’s me.

Short story: I’m trying to login to a website so I can download the fixtures for my youth team and add them to an app so my parents know if there is a game on or not. I’m currently doing this manually but I’ve started listening to the Automators podcast and I realise I should be doing more to automate my life.

I am trying to log into a website (username, password and submit button). Once in, I need to click a tab, select from a dropdown menu, gather the text and format it into a nice presentable way so I can add it to my app.

I’m currently having difficulties logging in. The form fields are not being filled out and the button is not being clicked. Here’s my code:

let user = 'email@email.com';
let pass = 'password';

let v = new WebView();
await v.loadURL('https://b2cthefa.b2clogin.com/b2cthefa.onmicrosoft.com/B2C_1A_signup_signinACTDyn/oauth2/v2.0/authorize?response_type=code&response_mode=query&redirect_uri=https%3A%2F%2Ffulltime-admin.thefa.com%2Fb2c%2FauthSignUpSignIn.html&client_id=16eece4b-82e1-4ae2-960d-45884ab3a895&scope=https%3A%2F%2Fb2cthefa.onmicrosoft.com%2FActScopeApp%2FWRITE+https%3A%2F%2Fb2cthefa.onmicrosoft.com%2FActScopeApp%2FREAD+https%3A%2F%2Fb2cthefa.onmicrosoft.com%2FActScopeApp%2Fuser_impersonation&prompt=select_account&state=40aef5dd-ef4b-4954-b1aa-bd2df9e909c9&nonce=3d44ccba-4c69-49df-914f-29900af65706&FAAppToken=FTA');

let js = `let user = '${user}';
let pass = ‘${pass}’;

document.getElementById('signInName').value = user;
document.getElementById('password').value = pass;

document.getElementById('next').click();`;

v.present(); v.evaluateJavaScript(js);
await v.waitForLoad();

const result = await v.evaluateJavaScript(js);

I’m currently testing this in Scriptable, it brings up the web page but that’s it. When I close the webpage, the loading bar at the bottom still continues meaning I have to close the script and start again.

What am I doing wrong?

I could be wrong, but from my experience, it’s best to load the url, then evaluate JS, then present web view. And need to await all three.

Example:

let wv = new WebView()
await wv.loadHTML(html)
await wv.evaluateJavaScript(js)
await wv.present(false)

Is it possible to show an example using my code above?

I assume that you’ve copied your script into your post. If this is the case then this line is probably the root of your problem:

You’re not using straight quotes '.

If this still doesn’t resolve your issue then it might be the case where the website not just simply accepts the value in a form field but somehow tracks also the key presses and don’t log in when none were registered (I don’t know why they are doing this).

Thank you.

That’s now stopped the error.

I now get a white screen with the scriptable loading icon. I’m guessing the script is working correctly but maybe doesn’t know what to do now?

Is this true and if so what’s my next option? I’m not sure if it is working or not.

What loading screen do you mean? Is the WebView just white? Do you mean the loading icon that replaces the script start button?


I thought that you removed some code here but if you haven’t then why are you executing the same code twice?

When you click play in scriptable, the play button changes to a loading button.

I didn’t realise I duplicated the code.

I’ve edited the code now and although I still can’t fill the form and submit it, I will check it on another site.

let user = 'email@email.com';
let pass = 'password';

let v = new WebView();
await v.loadURL('https://b2cthefa.b2clogin.com/b2cthefa.onmicrosoft.com/B2C_1A_signup_signinACTDyn/oauth2/v2.0/authorize?response_type=code&response_mode=query&redirect_uri=https%3A%2F%2Ffulltime-admin.thefa.com%2Fb2c%2FauthSignUpSignIn.html&client_id=16eece4b-82e1-4ae2-960d-45884ab3a895&scope=https%3A%2F%2Fb2cthefa.onmicrosoft.com%2FActScopeApp%2FWRITE+https%3A%2F%2Fb2cthefa.onmicrosoft.com%2FActScopeApp%2FREAD+https%3A%2F%2Fb2cthefa.onmicrosoft.com%2FActScopeApp%2Fuser_impersonation&prompt=select_account&state=40aef5dd-ef4b-4954-b1aa-bd2df9e909c9&nonce=3d44ccba-4c69-49df-914f-29900af65706&FAAppToken=FTA');

let js = `let user = '${user}';
let pass = '${pass}';

document.getElementById('signInName').value = 'dave@ddd.com';
document.getElementById('password').value = 'hgh';
`;

v.present(); v.evaluateJavaScript(js);

Seems to fill out the form on another site fine. When logging in, it uses auth2 in the url.

I’m not familiar with auth2 so could this be stopping the js filling out the form?

I’ve now tried your script. It looks like the elements are generated via javascript so they don’t exist yet when your auto-fill script is executed. You have to wait until the elements were created and then set their value.

2 Likes

Thank for looking at that.

I’ve looked at the scriptable docs and there appears to be a waitForLoad function but having played about all morning, I’m still not getting the script to work.

Any ideas?

The waitForLoad function only waits until the full HTML was parsed. If there is any async javascript then it won’t wait for that. You have to poll for the elements until they exist with evaluateJavaScript like

const v = new WebView();
await v.loadUrl(...);
await v.evaluateJavaScript(`
function check() {
  const el = document.getElementById("...");
  if (!el) setTimeout(check, 100);
  // el exists => log in
}

check();
`);
2 Likes

This makes a lot of sense! Thanks for the explanation!

I see a failure though that it can’t find that ID and it just stops the script

I also struggling to get this working?

I’ve been struggling to log in to the site so please excuse my late response.

I modified the script a bit further but I still get reference issues in the log:

2023-11-20 08:55:33: ReferenceError: Can’t find variable: documemt
2023-11-20 08:55:34: ReferenceError: Can’t find variable: documemt
2023-11-20 08:55:34: ReferenceError: Can’t find variable: documemt
2023-11-20 08:55:34: ReferenceError: Can’t find variable: documemt
2023-11-20 08:55:35: ReferenceError: Can’t find variable: documemt
2023-11-20 08:55:35: ReferenceError: Can’t find variable: documemt
2023-11-20 08:55:35: ReferenceError: Can’t find variable: documemt


let user = 'email@email.com';
let pass = 'password';

let v = new WebView();
await v.loadURL('https://b2cthefa.b2clogin.com/b2cthefa.onmicrosoft.com/B2C_1A_signup_signinACTDyn/oauth2/v2.0/authorize?response_type=code&response_mode=query&redirect_uri=https%3A%2F%2Ffulltime-admin.thefa.com%2Fb2c%2FauthSignUpSignIn.html&client_id=16eece4b-82e1-4ae2-960d-45884ab3a895&scope=https%3A%2F%2Fb2cthefa.onmicrosoft.com%2FActScopeApp%2FWRITE+https%3A%2F%2Fb2cthefa.onmicrosoft.com%2FActScopeApp%2FREAD+https%3A%2F%2Fb2cthefa.onmicrosoft.com%2FActScopeApp%2Fuser_impersonation&prompt=select_account&state=40aef5dd-ef4b-4954-b1aa-bd2df9e909c9&nonce=3d44ccba-4c69-49df-914f-29900af65706&FAAppToken=FTA');

await v.evaluateJavaScript(`
  function check() {
    try{const el = documemt.getElementById('password');
        // el exists => log in
      let user = '${user}';
      let pass = '${pass}';
      log(user);
      let a = document.getElementById('signInName');
      log(a.value);
      a.value = user;
      let b = document.getElementById('password');
      b.value = pass;
    }catch(e){
      log(e);
      setTimeout(check, 300);
    }

  }
  check();
`);

await v.present(false)



Edit: found the mix up with the error in the log. documemt doesn’t exist because of spelling in the initial if statement lol

let user = 'email@email.com';
let pass = 'password';

let v = new WebView();
await v.loadURL('https://b2cthefa.b2clogin.com/b2cthefa.onmicrosoft.com/B2C_1A_signup_signinACTDyn/oauth2/v2.0/authorize?response_type=code&response_mode=query&redirect_uri=https%3A%2F%2Ffulltime-admin.thefa.com%2Fb2c%2FauthSignUpSignIn.html&client_id=16eece4b-82e1-4ae2-960d-45884ab3a895&scope=https%3A%2F%2Fb2cthefa.onmicrosoft.com%2FActScopeApp%2FWRITE+https%3A%2F%2Fb2cthefa.onmicrosoft.com%2FActScopeApp%2FREAD+https%3A%2F%2Fb2cthefa.onmicrosoft.com%2FActScopeApp%2Fuser_impersonation&prompt=select_account&state=40aef5dd-ef4b-4954-b1aa-bd2df9e909c9&nonce=3d44ccba-4c69-49df-914f-29900af65706&FAAppToken=FTA');

await v.evaluateJavaScript(`
  function check() {
    try{const el = document.getElementById('password');
        // el exists => log in
      let user = '${user}';
      let pass = '${pass}';
      log(user);
      let a = document.getElementById('signInName');
      log(a.value);
      a.value = user;
      let b = document.getElementById('password');
      b.value = pass;
    }catch(e){
      log(e);
      setTimeout(check, 300);
    }

  }
  check();
`);

await v.present(false)
1 Like

Thanks

Adding click (directly under the line b.value = pass;) allows me try and login but I get an error on the website: Failed to validate data received from Authorization service - could not validate state.

When I login using Safari, I don’t get that error.

I tried to create an IOS Shortcut and run an inline script I get the following error: Presenting a configured web view is not supported.

I’ll be honest, I don’t know how to move on from here.

Presenting the webview isn’t possible in shortcuts. You shouldn’t need to present it though. My guess is that the url needs to be reduced for the cookies to generate properly. I could be wrong though.

Any idea how to run it and fix that issue in shortcuts?

All my shortcuts have been pretty simple up till now.

I think it can work in a shortcut if you don’t need the webview to show.

You could probably process all the info and then pass it off to shortcuts