excel 从上下文菜单selenium VBA中选择命令[保存为]

fjaof16o  于 2023-03-31  发布在  其他
关注(0)|答案(1)|浏览(77)

我在ExcelVBA中使用Selenium,我可以使用下面这行在元素上单击鼠标右键

bot.Actions.ClickContext(.FindElementById("Button3")).Perform

现在我如何从上下文菜单中选择命令Save As。我可以使用Application.SendKeys方法,但它不可靠。

9gm1akwq

9gm1akwq1#

尝试以下代码,它可以在不使用SEND_KEYS的情况下完成Save As ...菜单项的操作

Option Explicit

' Save As ...

Sub sbSaveAs()
    Dim driver As ChromeDriver
    Set driver = New ChromeDriver
    Dim sURL As String
    Dim we As WebElement
    sURL = "https://user10186832.wordpress.com/2023/03/08/simple-blog/"
    Dim sFilename As String
    Call driver.Start("edge")
    driver.get (sURL)
    driver.Window.Maximize
    sbDelay (100000)
    Set we = driver.FindElementByXPath("/html")
    sFilename = "C:\Users\david\Downloads\out-" & Format(Now(), "yyyymmddHHMMSS") & ".txt"
    Open sFilename For Output As #1
    Print #1, we.FindElementByXPath("/html").Attribute("outerHTML")
    Close #1
    driver.Quit
End Sub

Sub sbDelay(delay As Long): Dim i As Long: For i = 1 To delay:  DoEvents: Next i: End Sub 'old skool delay

相关问题