我正在尝试编写一个将仅登录this网站的宏。
我以前做过一些网页爬取工作,并且始终能够在其他网站上成功地完成类似的工作,但是我无法弄清楚这一点。
我已经尝试了很多事情,最后一次尝试(非常绝望!)是输入用户名和密码,并调用所有可能的脚本并触发所有可能的事件,请参见下面的代码。请忽略我的工作方式/命名方式(例如collections的eventsArray等)-我一直在尝试新事物并多次更改代码,这都是暂时的。
Dim eventsArray As New Collection
eventsArray.Add "abort"
eventsArray.Add "afterprint"
eventsArray.Add "animationend"
eventsArray.Add "animationiteration"
eventsArray.Add "animationstart"
eventsArray.Add "beforeprint"
eventsArray.Add "beforeunload"
eventsArray.Add "blur"
eventsArray.Add "canplay"
eventsArray.Add "canplaythrough"
eventsArray.Add "change"
eventsArray.Add "click"
eventsArray.Add "contextmenu"
eventsArray.Add "copy"
eventsArray.Add "cut"
eventsArray.Add "dblclick"
eventsArray.Add "drag"
eventsArray.Add "dragend"
eventsArray.Add "dragenter"
eventsArray.Add "dragleave"
eventsArray.Add "dragover"
eventsArray.Add "dragstart"
eventsArray.Add "drop"
eventsArray.Add "durationchange"
eventsArray.Add "ended"
eventsArray.Add "error"
eventsArray.Add "focus"
eventsArray.Add "focusin"
eventsArray.Add "focusout"
eventsArray.Add "fullscreenchange"
eventsArray.Add "fullscreenerror"
eventsArray.Add "hashchange"
eventsArray.Add "input"
eventsArray.Add "invalid"
eventsArray.Add "keydown"
eventsArray.Add "keypress"
eventsArray.Add "keyup"
eventsArray.Add "load"
eventsArray.Add "loadeddata"
eventsArray.Add "loadedmetadata"
eventsArray.Add "loadstart"
eventsArray.Add "message"
eventsArray.Add "mousedown"
eventsArray.Add "mouseenter"
eventsArray.Add "mouseleave"
eventsArray.Add "mousemove"
eventsArray.Add "mouseover"
eventsArray.Add "mouseout"
eventsArray.Add "mouseup"
eventsArray.Add "mousewheel"
eventsArray.Add "offline"
eventsArray.Add "online"
eventsArray.Add "open"
eventsArray.Add "pagehide"
eventsArray.Add "pageshow"
eventsArray.Add "paste"
eventsArray.Add "pause"
eventsArray.Add "play"
eventsArray.Add "playing"
eventsArray.Add "popstate"
eventsArray.Add "progress"
eventsArray.Add "ratechange"
eventsArray.Add "resize"
eventsArray.Add "reset"
eventsArray.Add "scroll"
eventsArray.Add "search"
eventsArray.Add "seeked"
eventsArray.Add "seeking"
eventsArray.Add "select"
eventsArray.Add "show"
eventsArray.Add "stalled"
eventsArray.Add "storage"
eventsArray.Add "submit"
eventsArray.Add "suspend"
eventsArray.Add "timeupdate"
eventsArray.Add "toggle"
eventsArray.Add "touchcancel"
eventsArray.Add "touchend"
eventsArray.Add "touchmove"
eventsArray.Add "touchstart"
eventsArray.Add "transitionend"
eventsArray.Add "unload"
eventsArray.Add "volumechange"
eventsArray.Add "waiting"
eventsArray.Add "wheel"
'set un and pw obj
Set user = IE.Document.all.user
Set pass = IE.Document.all.pass
'enter un and pw
user.Value = "test@test.com"
pass.Value = "test123"
'for each element
For Each ele In Array(user, pass)
'fire all poss events
For Each fEvent In eventsArray
On Error Resume Next
ele.FireEvent ("on" & fEvent)
ele.FireEvent (fEvent)
On Error GoTo 0
Next
Next
'exec all scripts availible in document
For Each scr In IE.Document.Scripts
On Error Resume Next
Call IE.Document.parentWindow.execScript(scr.src, "JavaScript")
On Error GoTo 0
Next
'sign in
IE.Document.getElementById("loginLink").Click
单击登录后会发生什么,返回“您缺少电子邮件地址”,两个值都消失。如果我在提交前手动修改了其中一个字段,其他值就会消失。
我尝试了一些随机的操作,例如添加事件侦听器,单击,聚焦以及不执行其他操作,甚至尝试在输入用户名和密码的每个字符后执行所有这些操作。我承认我只是盲目地尝试了所有这些事情,以希望某些事情能奏效,显然没有成功,我也不知道该怎么做。任何帮助将不胜感激!
/编辑: 我通过使用sendkeys(每个字段中的空格+退格键)获得了预期的结果,但是我不想这样做。当然有更好的方法吗?
答案 0 :(得分:1)
我将使用selenium basic vba(安装并确保最新的chromedriver在selenium文件夹中),然后使用vbe>工具>引用>添加硒类型库引用。干净得多。
Option Explicit
Public Sub Login()
Dim d As WebDriver
Set d = New ChromeDriver
With d
.Start "Chrome"
.get "https://uk.webuy.com/"
.FindElementByCss("#signIn").Click
.FindElementByCss("#user").SendKeys "abc@aol.com"
.FindElementByCss("#pass").SendKeys "password"
.FindElementByCss("#loginLink").Click
Stop
.Quit
End With
End Sub
答案 1 :(得分:0)
您尝试过这样的事情吗?
Sub cexLogIn()
Dim req As New WinHttpRequest
Dim reqBody As String
Dim reqURL As String
Dim respHeaders As String
Dim resp As String
reqURL = "https://wss2.cex.uk.webuy.io/v3/members/login"
reqBody = "{""password"":""PASSWORD"",""email"":""EMAIL@EMAIL.COM""}" 'use your credentials
With req
.Open "POST", reqURL, False
.setRequestHeader "Host", "wss2.cex.uk.webuy.io"
.setRequestHeader "Accept", "application/json, text/plain, */*"
.setRequestHeader "Accept-Language", "en-US,en;q=0.5"
.setRequestHeader "Accept-Encoding", "gzip, deflate, br"
.setRequestHeader "Referer", "https://uk.webuy.com/"
.setRequestHeader "Content-Type", "application/json;charset=UTF-8"
.send reqBody
resp = .responseText
respHeaders = .getAllResponseHeaders
End With
Debug.Print respHeaders
Debug.Print resp
End Sub
更改上限以匹配您的凭据。
通常,这应该使您登录,并且应该在响应头中将会话cookie返回给您。该Cookie可能会附加到您要对网站提出的任何其他请求。
我没有尝试过,因为我不想注册,但是您可以尝试一下,看看它如何进行。
参考:Microsoft WinHTTP Services, version 5.1