vba - VBA检查IE url是否已更改
问题描述
我一直在环顾四周,但似乎找不到这个问题的答案。我目前正在尝试使用 oauth2 连接到 API。
我可以很容易地在 IE 中显示登录屏幕。
Dim IE As InternetExplorer
Set IE = CreateObject("InternetExplorer.Application")
Dim url as string
url = "www.loginpage.com"
IE.Navigate url
IE.Visible = True
每当输入用户名和密码时,url 都会更改为带有授权码的重定向 url。我想将此重定向 url 存储到另一个变量,因此我可以减去授权代码并将其用于下一步。
我一直在尝试一个简单的如果,但到目前为止没有任何运气。第一个 url 变量填充了显示登录页面的 url。所以我想如果当前 url 与那个 url 不同,它可以存储在一个新变量中。可悲的是,我收到了运行时错误 80004005。也许你们知道更好的方法来做到这一点?
Dim url As String
Dim newurl As String
If IE.Document.url <> url Then
newurl = IE.Document.url
MsgBox (newurl)
End If
解决方案
你也可以ie.LocationURL
用来获取网址。我用 bing 做了一个例子,它可以很好地工作:
Sub LOADIE()
Set ieA = CreateObject("InternetExplorer.Application")
ieA.Visible = True
Dim url As String
url = "https://www.bing.com/"
ieA.navigate url
Do Until ieA.readyState = 4
DoEvents
Loop
Set doc = ieA.Document
doc.getElementById("sb_form_q").Value = "VBA"
doc.getElementById("sb_form_go").Click
Application.Wait (Now + TimeValue("00:00:05"))
Dim newurl As String
If ieA.LocationURL <> url Then
newurl = ieA.LocationURL
MsgBox (newurl)
End If
End Sub
结果:
推荐阅读
- c# - 无法将行添加到 TableLayoutPanel 插入索引超出范围
- arduino - 在 Arduino IDE 上运行代码时出现库错误
- ios - 从捆绑包中的 xcasset 加载图像和颜色代码
- ffmpeg - ffmpeg movflags 标志前的加号和减号有什么区别?
- math - 如何使 Ada 数学函数使用双精度(长浮点)变量?
- wordpress - 从动态 url(不是 json 文件)请求 wp_remote_get
- go - 跨 goroutine 同步值(计数器)
- assembly - MIPS - 为什么在调用子程序时恢复堆栈
- java - 在文件中调用方法
- docker - 从 Dockerfile 下载并安装 .sh