I'm trying to develop a new feature for our vb.net order entry system. At the moment I provide an assisted paypal login which loops through transactions and copies the transactions. My program then looks at this data and copies it into text boxes. The operator then approves and saves the record.
EDIT: I need to see the transaction in PayPal.
So my code uses IHTMLFormElement
and loops round form elements and adds values. However I only really use this to log in to paypal. See my code...
Dim theObject As Object = Nothing
theObject = "https://www.paypal.com/cgi-bin/w开发者_如何学编程ebscr?cmd=_login-run"
WebBrowPayPal.AxWebBrowser1.Navigate2(theObject)
While WebBrowPayPal.AxWebBrowser1.ReadyState <>
tagREADYSTATE.READYSTATE_COMPLETE
Application.DoEvents()
End While
Dim HtmlDoc As IHTMLDocument2 = CType(WebBrowPayPal.AxWebBrowser1.Document,
IHTMLDocument2)
Dim FormCol As IHTMLElementCollection = HtmlDoc.forms
Dim iForms As Integer = FormCol.length
Dim i As Integer
Dim x As Integer
For i = 0 To iForms - 1
Dim oForm As IHTMLFormElement = CType(FormCol.item(CType(i, Object),
CType(i, Object)), IHTMLFormElement)
For x = 0 To oForm.length - 1
If oForm.elements(x).tagname = "INPUT" Then
If oForm.elements(x).name = "login_email" Then
oForm.elements(x).value = "PayPal@mydomain.com"
End If
If oForm.elements(x).name = "login_password" Then
oForm.elements(x).value = "mypassword"
End If
If oForm.elements(x).type = "submit" Or _
oForm.elements(x).type = "SUBMIT" Then
oForm.elements(x).click()
End If
End If
Next
Next i
I'm now trying this page https://www.paypal.com/uk/cgi-bin/webscr?cmd=_history&nav=0.3.0
Which is the history page, which allows you to search on the paypal transaction id. Unfortunately you need to click on 'find a transaction' which then uses some javascript to shows the post fields. So the problem is that the fields I need to use are hidden.
How can I click on this javascript link in code ?
I'm not sure if this will help but you might want to try executing the script (which fires when you're clicking "Find a transaction") directly using IHTMLDocument2.write
method:
Dim HtmlDoc As IHTMLDocument2 = CType(WebBrowPayPal.AxWebBrowser1.Document,
IHTMLDocument2)
HtmlDoc.write("<script>[Search button event handler]</script>")
UPDATE
I managed to get it working. Here is a form Load
event handler (WinForms) that causes the Web Browser control to load http://www.google.com and then clicks "I'm feeling lucky" button:
Imports mshtml
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim clicked As Boolean = False
Dim doc As IHTMLDocument2 = Nothing
Dim form As IHTMLFormElement = Nothing
Dim input As HTMLInputElement = Nothing
Dim forms As IHTMLElementCollection = Nothing
AxWebBrowser1.Navigate2("http://www.google.com")
While AxWebBrowser1.ReadyState <> SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE
Application.DoEvents()
End While
doc = CType(AxWebBrowser1.Document, IHTMLDocument2)
For i As Integer = 0 To doc.forms.length - 1
form = CType(doc.forms.item(i, i), IHTMLFormElement)
For j As Integer = 0 To form.length - 1
If TypeOf (form.elements(j)) Is HTMLInputElement Then
input = CType(form.elements(j), HTMLInputElement)
If String.Compare(input.name, "btnI", StringComparison.InvariantCultureIgnoreCase) = 0 Then
input.click()
clicked = True
Exit For
End If
End If
Next
If clicked Then
Exit For
End If
Next
End Sub
End Class
If you need to click on a link then just change HTMLInputElement
to HTMLLinkElement
(in declaration and cast operation).
I suppose you're not using WinForms but if you do so, I'd suggest you to switch to the .NET version of the WebBrowser control.
Hope this will help you.
-- Pavel
精彩评论