2

i am trying to develop python GUI to access webpages. below example is working fine. but i need to pass user credentials(username/password) with in this code.

i dont want to click on that button. just i need to fill text boxes in login page

import wx 
import wx.html2 

class MyBrowser(wx.Dialog): 
  def __init__(self, *args, **kwds): 
    wx.Dialog.__init__(self, *args, **kwds) 
    sizer = wx.BoxSizer(wx.VERTICAL) 
    self.browser = wx.html2.WebView.New(self) 
    self.browser.LoadURL("http://wiki.python.org/moin/GuiProgramming?action=login") 
    sizer.Add(self.browser, 1, wx.EXPAND, 10) 
    self.SetSizer(sizer) 
    self.SetSize((700, 700)) 

if __name__ == '__main__': 
  app = wx.App() 
  dialog = MyBrowser(None, -1) 
  dialog.Show() 
  app.MainLoop() 
jamylak
  • 128,818
  • 30
  • 231
  • 230
AGR
  • 225
  • 1
  • 2
  • 16

3 Answers3

2

The "Use javascript" answer is certainly helpful, but with recent versions of wxPython anyway, it won't run unless wx.html2.EVT_WEB_VIEW_LOADED is changed to wx.html2.EVT_WEBVIEW_LOADED ("WEB_VIEW changed to "WEBVIEW").

David Schalk
  • 110
  • 4
  • This should be a comment of the answer with the code snippet. I finally came upon the answer myself and was just coming back here to update the answer with the info and saw this. – nmz787 Jul 19 '14 at 09:08
1

Use javascript. Simple sample code below.

import wx 
import wx.html2 

class MyBrowser(wx.Dialog): 
    def __init__(self, *args, **kwds): 
        wx.Dialog.__init__(self, *args, **kwds) 
        sizer = wx.BoxSizer(wx.VERTICAL) 
        self.browser = wx.html2.WebView.New(self) 
        self.browser.LoadURL("http://wiki.python.org/moin/GuiProgramming?action=login") 
        sizer.Add(self.browser, 1, wx.EXPAND, 10) 
        self.SetSizer(sizer) 
        self.SetSize((700, 700)) 

        # We have to bind an event so the javascript is only run once the page 
        # is  loaded.
        self.Bind(wx.html2.EVT_WEB_VIEW_LOADED, self.OnPageLoaded, 
                self.browser)


    def OnPageLoaded(self, evt):
        self.browser.RunScript("""
            // There are probably better ways to get the elements you
            // want, but this works.
            document.getElementsByName('name')[0].value="hist";
            document.getElementsByName('password')[0].value="bar";

            document.getElementById('openididentifier').value="ident";

            // If you want to submit the form you can use something like
            //document.getElementsByName('login')[1].click()
            """)

        # And you probably want to unbind the event here
        self.Bind(wx.html2.EVT_WEB_VIEW_LOADED, None, 
                self.browser)

if __name__ == '__main__': 
  app = wx.App() 
  dialog = MyBrowser(None, -1) 
  dialog.Show() 
  app.MainLoop() 
Ross
  • 206
  • 1
  • 6
  • Typo in above code... `EVT_WEB_VIEW_LOADED` should be `EVT_WEBVIEW_LOADED` – nmz787 Jul 19 '14 at 09:09
  • Think this was switched in one of the wxPython 2.9 releases, older versions use EVT_WEB_VIEW_LOADED, newer ones EVT_WEBVIEW_LOADED - obviously use which ever one works with your version or if you have to support both use a try/except block and catch the AttributeError. – Ross Jul 20 '14 at 09:50
-2

I would check out Selenium. It's an open source web navigating automation tool which has developed an awesome module for python. I've used it to automate logging into multiple different websites and you could easily slap a wx GUI on top of it.

John
  • 1