excel selenium VBA:driver.captureEntirePageScreenshot.Copy

ht4b089n  于 2023-03-13  发布在  其他
关注(0)|答案(2)|浏览(118)

新的论坛,新的 selenium VBA。道歉提前为我的无知。但是,我已经搜索了任何VBA版本的这一捕获整个页面元素没有插件或使用Java。
我想保持尽可能简单,捕捉一个完整的页面截图,然后粘贴到Excel表从剪贴板。
到目前为止,我知道它可以改进,有些可能是不必要的,但我不能告诉为什么它不会工作时,bot.TakeScreenshot.Copy确实工作,但只捕捉可见的屏幕。

Public Sub CommandButton1_Click()

Dim bot As New WebDriver, rng As Range
Set rng = Range(Worksheets("sheet1").Range("A2"), 
Worksheets("sheet1").Range("A2").End(xlDown))

'this loops through each href in cells navigating to the page unless the cell has end in it

For Each cell In rng
TextBox2.Text = ActiveCell.Text
TextBox3.Text = ActiveCell(1, 2).Text
If ActiveCell.Text = "End" Then
bot.Quit
Else
bot.Start "chrome", TextBox2.Text
bot.Window.Maximize
bot.Get "/"

'this takes a screenshot of each url in the loop and copys it

bot.captureEntirePageScreenshot.Copy

'bot.TakeScreenshot.Copy

'this creates a new sheet names it according the relavent test related to the url and pastes it in that newlt created sheet

ActiveWorkbook.Sheets.Add.Name = ActiveCell(1, 2).Text
ActiveSheet.PasteSpecial

'this goes back to the original sheet to continue with loop

Worksheets("sheet1").Select
ActiveCell.Offset(1, 0).Select

bot.Quit
End If
Next

End Sub
brccelvz

brccelvz1#

使用无头浏览器
这取自selenium附带的示例代码
在win7计算机上... C:\用户\xxxx\应用程序数据\本地\SeleniumBasic\示例\VBScript

Option Explicit

Sub demoPhantomJSDriver()

    Dim aaa As Selenium.PhantomJSDriver
    Set aaa = New Selenium.PhantomJSDriver
    aaa.Get "https://stackoverflow.com/questions/tagged/vba?sort=newest&pageSize=50"
    aaa.Window.Maximize

    Dim bbb As Object
    Set bbb = aaa.TakeScreenshot
    bbb.ToExcel Range("h3")
    Set bbb = Nothing

End Sub
mklgxw1f

mklgxw1f2#

除了来自@jsotola的答案外,我发现您不需要无头浏览器来完成此操作。

Option Explicit
Public myChrome as Selenium.ChromeDriver

Sub TakeScreenshot_Of_Page()
  Set myChrome = New Selenium.ChromeDriver 
  
  Dim URL as String
  URL="Https://YourSiteHere.com"
  
  myChrome.Start
  myChrome.Get URL
  
  MyChrome.TakeScreenshot.copy

End Sub

此外,如果需要,您可以只对一个对象进行屏幕截图:

'Uses same declaration as snip above

Sub TakeScreenshot_Of_Object(ByVal XPath_STR as String)
  'Assumes you are already on "YourSiteHere.com" from previous routine
  Dim FindBy as new Selenium.By
  If myChrome.IsElementPresent(FindBy.XPath("""" & XPath_STR & """") Then
    myChrome.FindElementByXpath("""" & XPath_STR & """").TakeScreenshot.Copy
    'Now Do what you want with that given element from the Xpath provided.
  End If
End Sub

相关问题