AutoIt v3: _IENavigate + _IELoadWait Not Working as Expected? - browser

This code is supposed to open a new browser set at "www.website.com," submit a username and password, wait for the page to load after being submitted, navigate to a new page, and inject a javascript code into the address bar.
My current results from this code are opening a new browser set at "www.website.com," submit a username and password. The submit works, then from here, the code breaks and instead of navigating to the next page (page2) it just hangs.
Even when I add an ignore command to the _IEFormSubmit($oForm) I can't get my page to navigate to the next page.
#include <IE.au3>
#RequireAdmin
Local $oIE = _IECreate("http://www.website.com")
;_IELinkClickByText($oIE, "Sign In") ;Optional
Local $oForm = _IEFormGetObjByName($oIE, "regular-user-login-form")
Local $oText = _IEFormElementGetObjByName($oForm, "log")
_IEFormElementSetValue($oText, "username")
Local $oText = _IEFormElementGetObjByName($oForm, "pwd")
_IEFormElementSetValue($oText, "password")
_IEFormSubmit($oForm)
_IENavigate($oIE, "http://www.website.com/page2/")
Send("{F4}javascript:check_in();{ENTER}")
Please for the love of god, what am I doing wrong.
Edit: I've also changed the _IEFormSubmit($oForm) to another javascript submit and I can still log in without any problems, but once I reach that next page I can't use _IENavigate, so the problem has to lie there.

I have automated so many different pages and sometimes the script gets stuck on _IEFormSubmit for no reason. That's an AutoIt bug.
Her's a quick fix for that
_IEFormSubmit($oForm, 0)
_IELoadWait($oIE, 1000)
Your code should be:
#include <IE.au3>
#RequireAdmin
Local $oIE = _IECreate("http://www.website.com")
;_IELinkClickByText($oIE, "Sign In") ;Optional
Local $oForm = _IEFormGetObjByName($oIE, "regular-user-login-form")
Local $oText = _IEFormElementGetObjByName($oForm, "log")
_IEFormElementSetValue($oText, "username")
Local $oText = _IEFormElementGetObjByName($oForm, "pwd")
_IEFormElementSetValue($oText, "password")
_IEFormSubmit($oForm, 0)
_IELoadWait($oIE, 1000)
_IENavigate($oIE, "http://www.website.com/page2/")
Send("{F4}javascript:check_in();{ENTER}")
Onetime, I tried to report two bugs to autoit forum, but the guys who are there are pretty frustrated fellas. So, now when I find one, I just solve it my way.
Off topic:
When dealing with objects you should know of another bug
If $oInput.outertext = "Continue" then ...
The above code will sometime fail even if the outertext matches.
This is solved using, use
If $oInput.outertext == "Continue" then ...

Related

Integrating selenium with 2captcha to solve Recapv2

I'm trying to make a script that autofills info for a specific form, I created that and it autofills fine but when I the script clicks on the enter button a captcha pops up, I decided to take the 2captcha route rather then the audio captcha bypass. I found a package that simplifies the 2cap API down for you(https://pypi.org/project/2captcha-python/#recaptcha-v2). I can send a request to 2captcha and they solve the captcha, I know this because my daily stats go up 1 everytime I run the script, but nothing happens on the selenium browser. Any reason?
current code is
solver = TwoCaptcha('MYAPIKEY')
config = {
'apiKey': 'MYAPIKEY',
'softId': 123,
'callback': 'https://your.site/result-receiver',
'defaultTimeout': 120,
'recaptchaTimeout': 600,
'pollingInterval': 10,
}
solver = TwoCaptcha(**config)
result = solver.recaptcha(sitekey='6Le-wvkSVVABCPBMRTvw0Q4Muexq1bi0DJwx_mJ-',
url="https://mailchi.mp/2d4364715d21/b66a2p7spo",)
I run this inside of a try: except: with the autofill code in it after It clicks enter(when the captcha pops up) any one have any ideas on how I can solve this? I've been trying for a couple hours and I can't figure it out.
Note: I left softId and callback as the default values because I don't have a softId from 2cap and I don't have a website either, if that's the issue please advise on how I can go about solving it.
Thanks in advance!

How to set window.alert when redirecting

I'm crawlling some web pages for my research.
I want to inject javascript code below when redirecting to other page:
window.alert = function() {};
I tried to inject the javascript code below using WebDriverWait, so that selenium may execute the code as soon as the driver redirect to new page. But It doesn't work.
while (some conditions) :
try:
WebDriverWait(browser, 5).until(
lambda driver: original_url != browser.current_url)
browser.execute_script("window.alert = function() {};")
except:
//do sth
original_url = browser.current_url
It seems that the driver execute javascript code after the page loaded because the alert that made in the redirected page is showing.
Chrome 14+ blocks alerts inside onunload (https://stackoverflow.com/a/7080331/3368011)
But, I think the following questions may help you:
JavaScript before leaving the page
How to call a function before leaving page with Javascript
JavaScript before leaving the page
I solved my problem in other way.
I tried and tried again with browser.switch_to_alert but it didn't work. Then I found that it was deprecated, so not works correctly. I checked the alert and dismiss it in every 1 second with following code :
while *some_condition* :
try:
Alert(browser).dismiss()
except:
print("no alert")
continue
This works very fine, in Windows 10, python 3.7.4

Node.js input password to a bin file being run via spawn

I am running a .bin file via child_process.spawn(), which takes in some inputs from the user before completing the setup. It seems to work fine taking all the inputs correctly via process.stdin.write("input"\n);. However, the same doesn't work when the password is sent via stdin. Directly running the bin file and manually entering the password works. Is there some format I am supposed to set before sending the password via node.js? I just keep seeing * being logged on stdout continuously and the setup doesn't seem to proceed further. Below is the snippet that I am using
var child_process = require('child_process');
var process = child_process.spawn('./test.bin');
process.stdout.on('data', function(data) {
if(data.toString().trim() === 'Username:')
process.stdin.write("test\n"); // This works
else if (data.toString().trim() === 'Password:')
process.stdin.write("password\n"); //This doesn't
Any inputs on the same might be helpful.Thanks.
Please note that when the password is entered by directly running the bin file, upon typing the password, nothing is displayed, but entering the correct password works. So, I am thinking there might be some encoding issues or something like that which I may be missing.
Answering my own question, it seemed to work by entering the password string one character at a time like below:
process.stdin.write(password[0]);
process.stdin.write(password[1]);
process.stdin.write(password[2]);
process.stdin.write(password[3]);
process.stdin.write("\n");

My program turns a spreadsheet into an Excel-file. But it only works for one user

I've made a larger (1000+ lines of code) App Script. It works great, for me. No other user can run it. I want them to be able to run it as well, and I can not figure out why they can't.
The problem occurs on this part:
var id = 'A_correct_ID_of_a_Google_Spreadsheet';
var SSurl = 'https://docs.google.com/feeds/';
var doc = UrlFetchApp.fetch(SSurl+'download/spreadsheets/Export?key='+id+'&exportFormat=xls',googleOAuth_('docs',SSurl)).getBlob();
var spreadsheet = DocsList.createFile(doc);
The function (and structure) was published here: other thread
function googleOAuth_(name,scope) {
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey('anonymous');
oAuthConfig.setConsumerSecret('anonymous');
return {oAuthServiceName:name, oAuthUseToken:"always"};
}
I can't see any reason why the program only would run for one user. All the files are shared between all the users and ownership have been swapped around.
When a script uses oAuth (googleOAuth_(name,scope) in you case) it needs to be authorized from the script editor, independently from the other authorization that the user grands with the "normal" usual procedure.
This has been the object of an enhancement request for quite a long time and has no valid workaround as far as I know.
So, depending on how your script is deployed (in a SS or a Doc or as webApp) you might find a solution or not... if, as suggested in the first comment on your post, you run this from a webapp you can deploy it to run as yourself and allow anonymous access and it will work easily, but in every other case your other users will have to open the script editor and run a function that triggers the oAuth authorization process.

How to click on onbeforeunload prompt in Watir test?

I have a webpage that has an onbeforeunload script that prompts the user when they take an action that would navigate away from the current page.
How do I interact with this popup using Watir? My current approach looks like this:
$ie.link(:text, 'Dashboard').click_no_wait
hwnd = $ie.enabled_popup(10)
assert(hwnd, 'The expected \'leave this page?\' popup was not shown')
win = WinClicker.new
win.makeWindowActive(hwnd)
win.clickWindowsButton_hwnd(hwnd, "OK")
The problem is that if I use "click no wait" the popup is not created, and the test times out. If I use "click" then the popup is created, but the test hangs after it opens.
Any suggestions?
Do you want to assert dialog message?
I try your code, but could not find solution.
This is to try to catch dialog then when popup,get msg while 5sec. polling per 1sec.
$ie.link(:text, 'Dashboard').click_no_wait
#autoit = WIN32OLE.new('AutoItX3.Control')
5.times do
if #autoit.WinWait('Windows Internet Explorer','',1)==1 then
#autoit.WinActivate('Windows Internet Explorer',"")
#dialog_text = #autoit.WinGetText('Windows Internet Explorer',"")
dialog_pop = "YES"
break
else
sleep(1)
dialog_pop = "NO"
end
end
This should do the trick closing this alert:
browser.alert.ok
See the Watir docs here.

Resources