wxpython,将用户数据传递给html2

时间:2013-05-09 11:20:44

标签: python wxpython wxwidgets wx.html2

我正在尝试开发python GUI来访问网页。以下示例工作正常。但我需要在此代码中传递用户凭据(用户名/密码)。

我不想点击那个按钮。我需要填写登录页面中的文本框

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() 

3 个答案:

答案 0 :(得分:2)

“使用javascript”答案肯定是有帮助的,但无论如何最新版本的wxPython除非将wx.html2.EVT_WEB_VIEW_LOADED更改为wx.html2.EVT_WEBVIEW_LOADED(“WEB_VIEW更改为”WEBVIEW“),否则它将无法运行。

答案 1 :(得分:1)

使用javascript。简单的示例代码如下。

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() 

答案 2 :(得分:-2)

我会查看Selenium。它是一个开源的web导航自动化工具,它为python开发了一个很棒的模块。我已经用它来自动登录到多个不同的网站,你可以轻松地在它上面打一个wx GUI。