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.
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).
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);
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.
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.
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();
`);
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)
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.