1

I am using selenium in excel VBA, and I could do the action of right-click on an element using this line

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

Now how can I select the command Save As from the context menu. I could use Application.SendKeys method but it is not reliable.

YasserKhalil
  • 9,138
  • 7
  • 36
  • 95
  • What do you want when you save the page? Just the html or an actual saved page with images and resources? You may need to use some sort of sendkeys action, looking at python/selenium questions you probably won't be able to show and automate the save as dialog without it. – NickSlash Mar 28 '23 at 11:24
  • Thanks. I have tried another approach using windows API at this link https://stackoverflow.com/questions/75865631/click-save-button-in-vba-using-windows-api but the problem persists. – YasserKhalil Mar 28 '23 at 11:25
  • What's your goal though? Do you need the whole page or just elements of it – NickSlash Mar 28 '23 at 11:34
  • I need to save the whole page as webpage, complete. – YasserKhalil Mar 28 '23 at 11:37
  • Here is the `SEND_KEYS` method. https://stackoverflow.com/questions/11428026/select-an-option-from-the-right-click-menu-in-selenium-webdriver-java?rq=2 – user10186832 Mar 28 '23 at 15:29
  • Please show your URL so that we could test our working code! – user10186832 Mar 28 '23 at 15:30
  • I already used SendKeys but you know it is not reliable approach. – YasserKhalil Mar 28 '23 at 16:05

1 Answers1

1

Try this code, it does what the Save As ... menu item does without using SEND_KEYS

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
user10186832
  • 423
  • 1
  • 9
  • 17
  • Very clever idea. But my target is not the HTML file only. With `Save As`, I can get the html content and a folder with associated files and I need those files. – YasserKhalil Mar 28 '23 at 19:45
  • @YasserKhalil indeed, perhaps you should ask another question and explain why you can't use `SEND_KEYS` and what you need from the associated files. – user10186832 Mar 29 '23 at 06:41
  • I can use SendKeys and I already solved the problem using SendKeys but you know it is not reliable, especially I will deal with a large number of web pages. And I need a specific file in the associated files named `saved_resource` and this is the most important one. – YasserKhalil Mar 29 '23 at 06:56
  • @YasserKhalil I can't see why `SEND_KEYS` would be unreliable. Why not use ctrl-S instead? – user10186832 Mar 29 '23 at 11:22
  • Can you explain what do you mean with using Ctrl keyboard? – YasserKhalil Mar 29 '23 at 11:24