我已经在VBA中结合IE编写了一个脚本,以填写两个输入字段来登录网页。
当我尝试执行脚本时,由于输入字段位于Access is denied
中,因此会引发 iframe
错误。如何用我的凭据填写两个输入字段?
这是我到目前为止的尝试:
Sub LogIn()
Const Url = "replace with above link"
Dim IE As New InternetExplorer, HTML As HTMLDocument
Dim frm As Object, post As Object
With IE
.Visible = True
.navigate Url
While .Busy Or .readyState < 4: DoEvents: Wend
Set HTML = .document
End With
Application.Wait Now + TimeValue("00:00:05")
Set frm = HTML.querySelector("iframe[name='disneyid-iframe']").contentWindow.document
Set post = frm.querySelector("input[placeholder='Username or Email Address']")
post.Value = "somename@gmailmail.com"
End Sub
您可以尝试使用任何输入详细信息。我只需要知道如何填充两个框即可。
与iframe
内容相关的HTML元素:
<iframe id="disneyid-secure-responder" name="disneyid-secure-responder" src="https://www.espn.com/disneyid/responder/index.html?clientId=ESPN-ONESITE.WEB-PROD&scheme=http&postMessageOrigin=http%3A%2F%2Ffantasy.espn.com%2Fbasketball%2Fteam%3FleagueId%3D24874190%26teamId%3D1%26statSplit%3DcurrSeason&cookieDomain=espn.com&config=PROD&logLevel=INFO&topHost=fantasy.espn.com&ageBand=ADULT&countryCode=CA&langPref=en-US&cssOverride=https%3A%2F%2Fsecure.espncdn.com%2Fcombiner%2Fc%3Fcss%3Ddisneyid%2Fcore.css%2Cdisneyid%2Ffantasy.css&responderPage=https%3A%2F%2Fwww.espn.com%2Fdisneyid%2Fresponder%2Findex.html&buildId=165f40c7564" style="display: none;"></iframe>
答案 0 :(得分:1)
如另一个答案所述,在这种情况下,iframe src
位于不同的域中,same domain policy有效地排除了版本8的IE。尽管搞砸了安全设置以尝试绕过这是一个坏主意,请参阅this进行阅读。
selenium basic对于使用Chrome的VBA来说非常容易。您还需要循环直到可以输入登录详细信息。我添加了超时以防止无限循环。
安装硒后,您需要进入VBE>工具>引用,并添加对硒类型库的引用。
Option Explicit
Public Sub Login()
Dim d As WebDriver, ele As WebElement, t As Date
Const MAX_WAIT_SEC As Long = 5
Const url = "http://fantasy.espn.com/basketball/team?leagueId=24874190&teamId=1&statSplit=currSeason"
Set d = New ChromeDriver
With d
.Start "Chrome"
.get url
t = Timer
Do
DoEvents
On Error Resume Next
.SwitchToFrame .FindElementById("disneyid-iframe")
Set ele = .FindElementByCss("[type=email]")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While Not ele.IsDisplayed
If ele Is Nothing Then Exit Sub
ele.SendKeys "Bob@Builder.com"
.FindElementByCss("[type=password]").SendKeys "password"
.FindElementByCss("[type=submit]").Click
Stop '<== Delete me later
'Other code
.Quit
End With
End Sub
答案 1 :(得分:1)
尝试参考下面的示例可能会解决您的问题。
主要HTML页面代码(demo51.html):
<!DOCTYPE html>
<html>
<body>
<H2>This is main page...</h2><br>
<iframe src="C:\Users\Administrator\Desktop\demo52.html" id="ifrm" height="300px" width="300px">
<p>Your browser does not support iframes.</p>
</iframe>
</body>
</html>
将在首页iframe(demo52.html)中显示的页面。
<!DOCTYPE html>
<html>
<body>
<h2> Sample Page</h2><br>
Name: <input type="text" id="txt_name" value=""><br><br>
Age:    <input type="text" id="txt_age" value=""><br><br>
            <input type="submit" value="submit">
</body>
</html>
VBA代码:
Public Sub demo()
Dim baseURL As String
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim workFrame As HTMLIFrame
Dim acctInput As HTMLInputElement
baseURL = "C:\Users\Administrator\Desktop\demo51.html"
Set IE = New InternetExplorer
With IE
.Visible = True
.Navigate baseURL
While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set workFrame = .Document.getElementById("ifrm")
Set HTMLdoc = workFrame.contentWindow.Document
End With
Set acctInput = HTMLdoc.getElementById("txt_name")
acctInput.Focus
acctInput.Value = "abcde"
Set acctInput = HTMLdoc.getElementById("txt_age")
acctInput.Focus
acctInput.Value = "25"
End Sub
Internet Explorer中的输出:
注意::尝试在VBA编辑器中添加对以下库的引用。
(1)Microsoft HTML对象库。
(2)Microsoft Internet控件。
尝试根据自己的要求更改代码中的URL。
答案 2 :(得分:0)
由于安全限制,您无法在其他域的iframe中进行更改。 http://en.wikipedia.org/wiki/Same_origin_policy