2

我正在尝试开发 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() 
4

3 回答 3

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").

于 2013-10-23T22:12:54.323 回答
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() 
于 2013-06-05T21:03:54.730 回答
-2

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

于 2013-10-24T22:14:55.793 回答