Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automation like in VBS won't work in VFP

Status
Not open for further replies.

JackTheC

Programmer
Feb 25, 2002
325
NL
I want to set the focus and the cursor in a specific inputbox of a website. Example Ebay's login.

In a VBS script that will work with something like this code:

Code:
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "[URL unfurl="true"]https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&ru=http%3A%2F%2Fwww.ebay.com%2F"[/URL]
ie.Visible = True

do while ie.readystate<>4
loop

ie.document.getElementbyId("pass").focus()
ie.document.getElementbyId("pass").select()

The same kind of code in VFP won't work. No errors but the focus stays in the default field.

Code:
ieo=createobject("InternetExplorer.Application")
ieo.navigate("[URL unfurl="true"]https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&ru=http%3A%2F%2Fwww.ebay.com%2F")[/URL]
ieo.visible=.t.

do while ieo.readystate<>4
     =inkey(0.1)
enddo

ieo.document.getElementbyId("pass").focus()
ieo.document.getElementbyId("pass").select()

How come? And is it possible to let it work properly directly in VFP? Of course I can execute a VBS from within VFP with ShellExecute but is it possible to make the above VFP code work?
 
Jack,

Your code is working perfectly for me.

In fact, what I am seeing is that the focus first goes to the user ID, but then immediately moves to the password. I can't off-hand see any reason for it not to work for you - unless there is some setting in your IE config that is causing it (but that's unlikely).

If I was writing this code, I wouldn't bother with the INKEY() - but I can't imagine that's relevant.

By the way, what do you mean by the "default field"?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks Mike.
The first code, the VBS works here too. Strange that the VFP code won't do its job here (Win8, IE10). I will test in on Win7 and Vista later. What OS/IE are you using?

The inkey(0.1) is not necessary. On my PC it works without too. I did it to give system resources to IE, to prevent VFP using 100% CPU on slower machines and give CPU time to IE. Something like Doevents in VB.

The default field is the field on the website that gets the focus or rather the cursor, when the page is loaded. At Ebays that is the Email/User ID.

 
Just a side note, instead of INKEY() there is DOEVENTS in VFP. DOEVENTS FORCE in VFP9. A final DOEVENTS mmight help. I'd not look for IE or OS Version causes in the first place, but VFP settings. For example, the _AUTOYIELD setting may explain a different behaviour in VFP than in VB.

Bye, Olaf.



 
What OS/IE are you using?

I tested it with IE 8 on Windows XP. I'll try with a later version on Windows 7 when I get time, but I don't expect it to make any difference.

Regarding the use of INKEY(), I note Olaf's point about DOEVENTS. But I doubt that it's relevant to this particular problem.

I see what you mean about the default field. That explains why I can see focus going to the user ID before it moves to the password.

Sorry I can't suggest anything else for the moment. If I have any more ideas, I'll get back.

Mike




__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike,

The loop may stay as is or not.

My idea would be testing:
Code:
ieo.document.getElementbyId("pass").select() 
DOEVENTS FORCE

The focus change should indeed happen without this, too, at least once the IE window is activated by the user, but DOEVENTS FORCE can cause a refresh of other apps, especially automated apps, which may not occur, if you omit it.

I use DOEVENTS FORECE after each CREATEOBJECT("...Word/Excel/Outlook/....Application") alone to ensure I can safely use that application object reference afterwards. Since Office 2007 you can fail on eg. addressing oWord.Documents collection or oExcel.Worksheets collection, because the CREATEOBBJECT returns with an object reference, while the OLE object tree still builds up and isn't yet accessable. This is a problem since Office 2007 for sure, not only since multi core CPUs or since Vista. Anyway, it helps in that case and it might help here, too, even no matter how _AUTOYIELD is set.

Bye, Olaf.
 
...why don't I simply try before writing...

No, DOEVENTS FORCE doen't help here.

In the first place, I don't see the focus change, too, on VFP9 SP2 with Win8.1 and IE11

On the global scale, browsers get more and more resistant on automating them and that's a good practice from the user's safety point of view. There are several APIs to use:
Bye, Olaf.
 
Tested on Win7: No focus change unless I alter the code: IE must be in the foreground. Than it worked in Win7
But alas, still not working in Win8 and Vista either.

Code:
declare SetForegroundWindow IN win32api INTEGER

ieo=createobject("InternetExplorer.Application")
ieo.navigate("[URL unfurl="true"]https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&ru=http%3A%2F%2Fwww.ebay.com%2F")[/URL]
ieo.visible=.t.

hw=ieo.hwnd
SetForegroundWindow(hw)

do while ieo.readystate<>4
	doevents
enddo

ieo.document.getElementbyId("pass").focus()
ieo.document.getElementbyId("pass").select()
Thanks for the info about doevents instead off inkey(). But as you noticed that is not the issue here.

I would like to distribute a program that incorperates the discussed code to users that have Vista, Win7 or Win8 installed on their machines. But when the code behaves irratic I can not do that of course.
 
Oh and by the way. Ebay.com is just an example of an international known website. The code must work on many (perhaps every) website. Personally I never used ebay, just took it by example.
 
I would like to distribute a program that incorperates the discussed code to users that have Vista, Win7 or Win8 installed on their machines. But when the code behaves irratic I can not do that of course.

What exactly is the end goal here? I was assuming you simply want to let the user type the password without them having to manually move focus to the password field. But, if that was the case, that wouldn't prevent you distributing the app to Windows 8 users. The app wouldn't be behaving erratically. It would just be a minor inconvenience.

Is your goal to actually enter the password programmatically? If so, it would probably be easier to programmatically set the value of ieo.document.getElementbyId("pass").

Mike




__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
The code must work on many (perhaps every) website. Personally I never used ebay, just took it by example.

Ummm ... not all websites have an element named "pass".

Like Mike I'm wondering what the actual goal is here. If it's generic web browsing by controlling unknown browsers on unknown sites I don't think you'll find an answer. If it's something more specific there may be some other way to crack the nut.

But as Olaf said, scripting the browser is no longer considered a normal way of doing things. It's a security risk. Look for it to get more difficult as time marches on.
 
Guys this was just a simplified example for the forum here. To demonstrate the problem. The actual program is much more complicated and works with variables and databases etc.. That is NOT the discussion. I just came across this behaviour where VFP acts different SOMETIMES then VBS. And Yes Mike thank you, I knew about the value feature, I use that too in the program. I want just diffent methods for doing the same depending on the website and the users preferences.
 
In fact there are websites that have NO default input field. Bad programmed websites where the developer forgot to set focus to the first field. In that case I want to set focus to the first input field. But now I am discussing the reason and that was not the purpose of the question.
 
You might get a good discussion over on Rick Strahl's message board at west-wind.com because a lot of people who do extensive work with websites and browsers hang out there.
 
I take your point about it not being the purpose of the question. But you did say that you didn't want to distribute your app because of "erratic behaviour". I still don't see that the failure to set focus in a given field in some cases will prevent you from distributing the app. I can see that it is inconsistent behaviour, but it's hardly likely to be a show-stopper.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Forget about the ebay API then, anyway I con't think of anything VFP would cause different than VB. You could put that part of your code into a VB component, then.

Bye, Olaf.
 
Olaf, its VBS not VB. And in my first entry I already suggested "Of course I can execute a VBS from within VFP with ShellExecute".
So I think that I do that. But VBscript has to work on each PC then. When I just tested on my Vista machine, VBS wouldn't work because the scripting engine could not be found. After intensive research it seemed that a very old instance of Antivirus program, which was not correctly uninstalled bij its uninstall program left code behind preventing VBS to work. There was no way of uninstalling. Still isn't.
This is an example of how tricky it is to depend on external program working correctly on each PC. So I would prefer if everything could be done by VFP.
 
Jack, be aware that VBS is quite an old technology now. Because it is browser-specific, it has to a large extent been overtaken by Javascript.

Another point. I understand that eBay is just an example. But what about the element ID ("pass" in your code)? Will that be hard-coded, or do you plan to pass it as a parameter, along with the URL. The reason I mention it is that if your target page does not have an element with that ID, you will get a runtime error in VFP ("GetElementByID does not evaluate to an object"). That's true regardless of the OS or IE version.

That might not be relevant, but I thought I'd mention it.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Of course Mike. The real values are in a DBF table. Pass is only on ebay.coms login. And just in this example. For demonstration purpose only.
With ie.document.activeElement.id you can determine the actual value of a website (I hope).
And XP Mike, is quite an old technology now. You should not use that anymore. Its not safe since it will not be updated by Microsoft.
 
Well, no matter if vb module or vbs script. It's unfortunate of course, if for some reasons .vbs scripts won't run.
Beside, Mike, .vbs scripts really are a thing separate of <script language="vbscript"> inside browsers.

I'd perhaps go for powershell scripts, if doing something separate of VFP rooted in the pure OS resources.
Though that has it's hurdles, too. I found this quite instructive:
If you don't trust anything coming from windows itself, autohotkey is a nice thing to use:
[URL unfurl="true"]http://www.autohotkey.com/board/topic/56987-com-object-reference-autohotkey-v11/[/url]

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top