我有以下代码导航到网站,登录,然后保存显示的网页数据。但是我看不到保存到.txt文件中的html.body.innerHTML
中的表。我看到下面的div
内容。当我检查元素时,它显示表格在嵌套的div
元素下,这些元素在div
下,我想打印到EXCEL。如果能有任何帮助来实现这一点,我将不胜感激。
<div id="container_div" style="height: 100%;">
<table style="width: 100%; height: 100%;">
<tbody><tr>
<td align="center" valign="middle">
<img alt="progress" src="/ria/images/progress.gif">
<br>
<span>Loading...</span>
</td>
</tr>
</tbody></table>
</div>
VBA代码:
Option Explicit
Private ch As Selenium.ChromeDriver
Sub TestSelenium()
Set ch = New Selenium.ChromeDriver
ch.Start
ch.Get "https://siteaddress"
ch.Timeouts.ImplicitWait = 20000 ' 5 seconds
With ch
.AddArgument "--headless" ''This is the fix
With .FindElementById("logInForm")
.FindElementById("j_username").SendKeys "username"
.FindElementById("j_password").SendKeys "password"
.FindElementById("submitButton", timeout:=1000000).Click
'ch.Timeouts.Server = 120000 ' 2 mins
'ch.Timeouts.ImplicitWait = 50000 ' 5 seconds
End With
'Stop '<== Delete me after inspection
' .SwitchToFrame .FindElementByTag("iframe", timeout:=10000) '' switch to iframe
Dim html As New HTMLDocument ' Requires Microsoft HTML Library
html.body.innerHTML = ch.ExecuteScript("return document.body.innerHTML;")
Debug.Print html.body.innerText
Dim filePath As String
filePath = "C:\temp\MyTestFile.txt"
' The advantage of correctly typing fso as FileSystemObject is to make autocompletion
' (Intellisense) work, which helps you avoid typos and lets you discover other useful
' methods of the FileSystemObject
Dim fso As FileSystemObject
Set fso = New FileSystemObject
Dim fileStream As TextStream
' Here is another great method of the FileSystemObject that checks if a file exists
If fso.FileExists(filePath) Then
fso.DeleteFile filePath
End If
' Here the actual file is created and opened for write access
Set fileStream = fso.CreateTextFile(filePath)
' Write something to the file
fileStream.WriteLine html.body.innerHTML
' Close it, so it is not locked anymore
fileStream.Close
.Quit
End With
End Sub
1条答案
按热度按时间aurhwmvo1#
我无法找到遍历所有表的所有循环的解决方案,但使用以下代码使用完整的
XPath
从特定表中提取数据: