Wxpython,将用户数据传递给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() 

"使用javascript"的答案当然是有帮助的,但无论如何,在wxPython的最新版本中,它不会运行,除非wx.html2。EVT_WEB_VIEW_LOADED更改为wx.html2。EVT_WEBVIEW_LOADED ("WEB_VIEW改变为"WEBVIEW")。

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

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

相关内容

  • 没有找到相关文章

最新更新