Antwise community forums
VFP Web automation and form filling - Printable Version

+- Antwise community forums (https://forums.antwise.com)
+-- Forum: Antview (https://forums.antwise.com/forumdisplay.php?fid=11)
+--- Forum: Antview for Windows (https://forums.antwise.com/forumdisplay.php?fid=12)
+--- Thread: VFP Web automation and form filling (/showthread.php?tid=175)



VFP Web automation and form filling - ozcan - 2023-11-07

I want to fill out a form on a website using VFP. 
I can fill in the textboxes in the form with this code:

Code:
thisform.oAntviewDocument = CREATEOBJECT('AntViewAx.AntViewDocument')
thisform.oAvdCtrl = NEWOBJECT('avdctrl','avdctrl.prg')
EVENTHANDLER(this.oAntviewDocument,this.oAvdCtrl)
thisform.oAntviewDocument.CurrentBrowser = thisform.oAntView.object
thisform.oAntView.object.navigate(ALLTRIM(thisform.text1.Value))

ThisForm.OAntViewDocument.ElementValueByName("txtSifre") = SIFRE  && Filling text

I have two questions:
 
1. How can I click on a link in the form with which code? (Picture1)
   
2. How can I check a checkbox in the web form? (Picture2)  
   

Thank you.


RE: VFP Web automation and form filling - wila - 2023-11-07

Hello Ozcan,

Here's some example code in VFP to do what you asked.
This is based on the demo code that is pre-installed.

First some code to automatically open the website in the init procedure of the form.

Code:
thisform.oAntviewDocument = CREATEOBJECT('AntViewAx.AntViewDocument') && create the AntViewDocument and assign it to a form property
thisform.oAvdCtrl = NEWOBJECT('avdctrl','avdctrl.prg') && CREATEOBJECT("Ctrl") && We need an object for our events
EVENTHANDLER(this.oAntviewDocument,this.oAvdCtrl)

thisform.oAntView.Init()
thisform.oAntView.UnlockControl("ExampleCompany","WI5PO2-2KSU3Q-HWFXFU-IUMU2V-QF8P2F")
lnStatus = thisform.oAntView.UnlockStatus

thisform.oAntviewDocument.CurrentBrowser = thisform.oAntView.object && this property shows as "Write-only" in the object browser

thisform.text1.Value = [https://eokulyd.meb.gov.tr/]
thisform.oAntView.object.navigate(ALLTRIM(thisform.text1.Value))

To automatically click on the link "button" I've written the following bit in the click method of the LoadString button:
Code:
LOCAL lcjScript
lcjScript = ''
TEXT TO lcjScript NOSHOW PRETEXT 7
() => {
       let info, row, column, link
       info = document.getElementById('information');
       row = info.firstElementChild;
       column = row.firstElementChild;
       link = column.firstElementChild;
       if (link) {
         link.click();
       }
     }
ENDTEXT

thisform.oAntViewDocument.RunAnonymousFunction( 1, "", lcjScript)

Normally you should check that each of those get element object methods actually returns an object.
So the error handling is missing I expect that you can write that yourself.

It clicked the left hand "button" during my tests.

For "checking" a checkbox in a form I added the following to the "go" button click method:


Code:
LOCAL lcjScript
LOCAL lbIsSuccess
LOCAL leStatus
lcjScript = ''
TEXT TO lcjScript NOSHOW PRETEXT 7
() => {
       let obj
       obj = document.getElementById('auto');
       if (obj) {
         obj.setAttribute("checked","");
       }
     }
ENDTEXT

thisform.oAntView.navigateSync("https://antwise.com/demo/SimpleCheckboxExample.html",lbIsSuccess,leStatus)

thisform.oAntViewDocument.RunAnonymousFunction( 1, "", lcjScript)


This first navigates to the page https://antwise.com/demo/SimpleCheckboxExample.html which has 3 checkboxes with all of them unchecked. Then it checks the "I have a car" checkbox immediately after loading the page.

This last example makes it clear that it would be really great to have a method for setting a element attribute in the document interface without having to resort to javascript in your code.
Which will be looked into.

Hope this helps,
--
Wil


RE: VFP Web automation and form filling - ozcan - 2023-11-08

The examples you provided are very valuable, thank you very much for your support.
In the code mentioned below, what should I do to click on the 2nd or 3rd button instead of the first button?
{
      let info, row, column, link
      info = document.getElementById('information');
      row = info.firstElementChild;
      column = row.firstElementChild;
      link = column.firstElementChild;
      if (link) {
        link.click();
      }
    }
If you have a link to more demos prepared for VFP, I would be happy if you could share it. Thanks again.


RE: VFP Web automation and form filling - wila - 2023-11-08

Hi,

There are multiple ways to do that.
First look at the html again.
   

As you can see in the image above, the second button is the 2nd link element within the div column element.

You could for example use the javascript nextElementSibling to get the object for the 2nd element.

Code:
LOCAL lcjScript
lcjScript = ''
TEXT TO lcjScript NOSHOW PRETEXT 7
() => {
       let info, row, column, link
       info = document.getElementById('information');
       row = info.firstElementChild;
       column = row.firstElementChild;
       link = column.firstElementChild;
       if (link) {
         link = link.nextElementSibling;
         link.click();
       }
     }
ENDTEXT

thisform.oAntViewDocument.RunAnonymousFunction( 1, "", lcjScript)

Getting the 3rd or 4th etc.. element would be a matter of "next next" until you reached the element of interest.
Do-able for the 3rd element I guess, but beyond that you'll get code that is hard to read/understand.

In that case it might be better to enumerate all the child objects and access them by index, by using the javascript children method.
The code then could look like this:
Code:
LOCAL lcjScript
lcjScript = ''
TEXT TO lcjScript NOSHOW PRETEXT 7
() => {
       let info, row, column, links
       info = document.getElementById('information');
       row = info.firstElementChild;
       column = row.firstElementChild;
       links = column.children;
       if (links.length>0) {
         links[1].click();
       }
     }
ENDTEXT

thisform.oAntViewDocument.RunAnonymousFunction( 1, "", lcjScript)

More VFP example code would be great and it is on our wish list, but it will probably take some more time as we are working on the control itself. But don't hesitate to ask here if you need assistance and we'll be happy to provide code snippets.

--
Wil


RE: VFP Web automation and form filling - ozcan - 2023-11-08

(2023-11-08, 15:05:52)wila Wrote: Hi,

There are multiple ways to do that.
First look at the html again.


As you can see in the image above, the second button is the 2nd link element within the div column element.

You could for example use the javascript nextElementSibling to get the object for the 2nd element.

Code:
LOCAL lcjScript
lcjScript = ''
TEXT TO lcjScript NOSHOW PRETEXT 7
() => {
       let info, row, column, link
       info = document.getElementById('information');
       row = info.firstElementChild;
       column = row.firstElementChild;
       link = column.firstElementChild;
       if (link) {
         link = link.nextElementSibling;
         link.click();
       }
     }
ENDTEXT

thisform.oAntViewDocument.RunAnonymousFunction( 1, "", lcjScript)

Getting the 3rd or 4th etc.. element would be a matter of "next next" until you reached the element of interest.
Do-able for the 3rd element I guess, but beyond that you'll get code that is hard to read/understand.

In that case it might be better to enumerate all the child objects and access them by index, by using the javascript children method.
The code then could look like this:
Code:
LOCAL lcjScript
lcjScript = ''
TEXT TO lcjScript NOSHOW PRETEXT 7
() => {
       let info, row, column, links
       info = document.getElementById('information');
       row = info.firstElementChild;
       column = row.firstElementChild;
       links = column.children;
       if (links.length>0) {
         links[1].click();
       }
     }
ENDTEXT

thisform.oAntViewDocument.RunAnonymousFunction( 1, "", lcjScript)

More VFP example code would be great and it is on our wish list, but it will probably take some more time as we are working on the control itself. But don't hesitate to ask here if you need assistance and we'll be happy to provide code snippets.

--
Wil

Thank you very much.  
Shy


RE: VFP Web automation and form filling - ozcan - 2023-11-10

(2023-11-08, 17:07:34)ozcan Wrote:
(2023-11-08, 15:05:52)wila Wrote: Hi,
There are multiple ways to do that.
First look at the html again.
As you can see in the image above, the second button is the 2nd link element within the div column element.
You could for example use the javascript nextElementSibling to get the object for the 2nd element.
Code:
LOCAL lcjScript
lcjScript = ''
TEXT TO lcjScript NOSHOW PRETEXT 7
() => {
       let info, row, column, link
       info = document.getElementById('information');
       row = info.firstElementChild;
       column = row.firstElementChild;
       link = column.firstElementChild;
       if (link) {
         link = link.nextElementSibling;
         link.click();
       }
     }
ENDTEXT
thisform.oAntViewDocument.RunAnonymousFunction( 1, "", lcjScript)

Getting the 3rd or 4th etc.. element would be a matter of "next next" until you reached the element of interest.
Do-able for the 3rd element I guess, but beyond that you'll get code that is hard to read/understand.
In that case it might be better to enumerate all the child objects and access them by index, by using the javascript children method.
The code then could look like this:
Code:
LOCAL lcjScript
lcjScript = ''
TEXT TO lcjScript NOSHOW PRETEXT 7
() => {
       let info, row, column, links
       info = document.getElementById('information');
       row = info.firstElementChild;
       column = row.firstElementChild;
       links = column.children;
       if (links.length>0) {
         links[1].click();
       }
     }
ENDTEXT
thisform.oAntViewDocument.RunAnonymousFunction( 1, "", lcjScript)

More VFP example code would be great and it is on our wish list, but it will probably take some more time as we are working on the control itself. But don't hesitate to ask here if you need assistance and we'll be happy to provide code snippets.
--
Wil

Thank you very much.  
Shy



The code I wrote to click the button with the "Listele" Id on the web form and submit it is not working. I wonder where I might be making a mistake?

  thisform.oAntViewDocument.RequestFormSubmitById("btnListele")

   


RE: VFP Web automation and form filling - wila - 2023-11-10

Hi Ozcan,

Your code looks fine to me.
Is the input control part of a html form?

Do you have a link to the page above that I can try and see what is happening?
--
Wil


RE: VFP Web automation and form filling - ozcan - 2023-11-13

(2023-11-10, 22:04:34)wila Wrote: Hi Ozcan,

Your code looks fine to me.
Is the input control part of a html form?

Do you have a link to the page above that I can try and see what is happening?
--
Wil


Thank you very much Wil.

This website is accessed through two-factor authentication system. 
I am sending the entire HTML content (and screenshot of inspect element) in the attachment. 
I want to click "Listele" button and submit the form.
Thank you.


RE: VFP Web automation and form filling - wila - 2023-11-13

Ozcan,

Thanks for the html.

For some reason the default requestSubmit function isn't working. Will try to figure out why that is.

The following however might be able to work around the issue.

Code:
LOCAL lcjScript
LOCAL lbIsSuccess
LOCAL leStatus As Long
LOCAL leError As Integer
lcjScript = ''
TEXT TO lcjScript NOSHOW PRETEXT 7
() => {
       let listele
       listele = document.getElementById('btnListele');
       if (listele) {
         listele.click();
       }
     }
ENDTEXT
thisform.oAntView.navigateSync("https://antwise.com/demo/20231113-formSubmitButton.html",lbIsSuccess,leStatus)
thisform.oAntViewDocument.RunAnonymousFunction( 1, "", lcjScript)

Your button has a onclick event, so instead of triggering submit, we're triggering the onclick event by programmatically sending a click.

Hope this helps,
Wil


RE: VFP Web automation and form filling - wila - 2023-11-13

Ozcan,

Got some more time to look into this (busy day).

The reason it doesn't work, is because the form is using method type "post".
This shouldn't be a problem and is considered a bug. 
Let me know if the workaround I supplied works for you and we'll add some functionality to handle this automatically.
--
Wil


RE: VFP Web automation and form filling - carlos@agpsoftware.com - 2023-11-24

(2023-11-07, 18:26:06)wila Wrote: Hello Ozcan,

Here's some example code in VFP to do what you asked.
This is based on the demo code that is pre-installed.

First some code to automatically open the website in the init procedure of the form.

Code:
thisform.oAntviewDocument = CREATEOBJECT('AntViewAx.AntViewDocument') && create the AntViewDocument and assign it to a form property
thisform.oAvdCtrl = NEWOBJECT('avdctrl','avdctrl.prg') && CREATEOBJECT("Ctrl") && We need an object for our events
EVENTHANDLER(this.oAntviewDocument,this.oAvdCtrl)

thisform.oAntView.Init()
thisform.oAntView.UnlockControl("ExampleCompany","WI5PO2-2KSU3Q-HWFXFU-IUMU2V-QF8P2F")
lnStatus = thisform.oAntView.UnlockStatus

thisform.oAntviewDocument.CurrentBrowser = thisform.oAntView.object && this property shows as "Write-only" in the object browser

thisform.text1.Value = [https://eokulyd.meb.gov.tr/]
thisform.oAntView.object.navigate(ALLTRIM(thisform.text1.Value))

To automatically click on the link "button" I've written the following bit in the click method of the LoadString button:
Code:
LOCAL lcjScript
lcjScript = ''
TEXT TO lcjScript NOSHOW PRETEXT 7
() => {
       let info, row, column, link
       info = document.getElementById('information');
       row = info.firstElementChild;
       column = row.firstElementChild;
       link = column.firstElementChild;
       if (link) {
         link.click();
       }
     }
ENDTEXT

thisform.oAntViewDocument.RunAnonymousFunction( 1, "", lcjScript)

Normally you should check that each of those get element object methods actually returns an object.
So the error handling is missing I expect that you can write that yourself.

It clicked the left hand "button" during my tests.

For "checking" a checkbox in a form I added the following to the "go" button click method:


Code:
LOCAL lcjScript
LOCAL lbIsSuccess
LOCAL leStatus
lcjScript = ''
TEXT TO lcjScript NOSHOW PRETEXT 7
() => {
       let obj
       obj = document.getElementById('auto');
       if (obj) {
         obj.setAttribute("checked","");
       }
     }
ENDTEXT

thisform.oAntView.navigateSync("https://antwise.com/demo/SimpleCheckboxExample.html",lbIsSuccess,leStatus)

thisform.oAntViewDocument.RunAnonymousFunction( 1, "", lcjScript)


This first navigates to the page https://antwise.com/demo/SimpleCheckboxExample.html which has 3 checkboxes with all of them unchecked. Then it checks the "I have a car" checkbox immediately after loading the page.

This last example makes it clear that it would be really great to have a method for setting a element attribute in the document interface without having to resort to javascript in your code.
Which will be looked into.

Hope this helps,
--
Wil

Hi.
Sorry. Why this text '() =>' before brackets?
It's beeing part of the  lcjScript variable.
I don't understand that.


RE: VFP Web automation and form filling - wila - 2023-11-24

(2023-11-24, 18:39:33)carlos@agpsoftware.com Wrote: Hi.
Sorry. Why this text '() =>' before brackets?
It's beeing part of the  lcjScript variable.
I don't understand that.

Hello Carlos,

Don't be confused, it's a short hand notation detail from javascript. 
It's called an arrow function.

The runAnonymousFunction depends on something that is called anonymous functions in javascript.
So basically a function without a name.
You can still pass variables though, that's why the () is there, so you can pass variables in there. (Click on the runAnonymousFunction link to see an example)

More info about that can be read here:
https://www.w3schools.com/js/js_function_definition.asp
and here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions
direct link in MDN:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

--
Wil


RE: VFP Web automation and form filling - carlos@agpsoftware.com - 2023-11-27

(2023-11-24, 19:07:01)wila Wrote:
(2023-11-24, 18:39:33)carlos@agpsoftware.com Wrote: Hi.
Sorry. Why this text '() =>' before brackets?
It's beeing part of the  lcjScript variable.
I don't understand that.

Hello Carlos,

Don't be confused, it's a short hand notation detail from javascript. 
It's called an arrow function.

The runAnonymousFunction depends on something that is called anonymous functions in javascript.
So basically a function without a name.
You can still pass variables though, that's why the () is there, so you can pass variables in there. (Click on the runAnonymousFunction link to see an example)

More info about that can be read here:
https://www.w3schools.com/js/js_function_definition.asp
and here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions
direct link in MDN:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

--
Wil

I see. Thanks


RE: VFP Web automation and form filling - wila - 2023-11-28

Thank you for asking as it made it clear that the documentation could use a little bit extra info about arrow functions.
This has been added and will be in the next update.
--
Wil