VBA Selenium download PDF without dialog - vba

I try to open Chrome with some options
Set driver = New ChromeDriver
driver.AddArgument "--kiosk-printing"
driver.SetPreference "download.prompt_for_download", False
Kiosk-printig works fine, but when I try to save/download the PDF opened in Chrome, the file save dialog still opens.
What am I doing wrong?
UPDATE:
Public Sub browser_open()
Set driver = New ChromeDriver
driver.SetPreference "download.default_directory", "C:\PDF_folder\"
driver.SetPreference "download.directory_upgrade", True
driver.SetPreference "download.prompt_for_download", False
driver.AddArgument "--kiosk-printing"
driver.Start "chrome"
driver.get "https://data2.manualslib.com/pdf/7/642/64176-haier/wm6002a.pdf?80517c2674ab60981d9ebd32535ca98d"
End Sub

Related

VBA Selenium Chromedriver save PDF

This link is a PDF file that opens in Chrome PDF file.
Can I somehow save this PDF file from this page?
I run chrome with such settings
driver.SetPreference "download.default_directory", "c:\chr"
driver.SetPreference "download.directory_upgrade", True
driver.SetPreference "download.prompt_for_download", False
but when you press CONTROL + S, the save as window still appears.
May be i can download PDF directly from Chrome?
UPDATE CODE:
This works fine:
Public Sub browser_open()
Set driver = New ChromeDriver
driver.SetPreference "download.prompt_for_download", False
driver.AddArgument "--kiosk-printing"
driver.Start "chrome"
driver.get "https://data2.manualslib.com/pdf3/53/5221/522008-haier/washing_machine.pdf?b76112ef24159605ca8df71689bce0a7"
driver.SendKeys keys.ArrowDown
driver.SendKeys keys.ArrowDown, keys.ArrowLeft
But if i want to press "CONTROL + S":
driver.SendKeys keys.Control, "s"
nothing happens.
I apologize for my English, I use Google Translate.
This command runs a script that creates a link element to save the page as a pdf file, and then simulates clicking on the link.
driver.ExecuteScript "var a = document.createElement('a'); a.href = '" & driver.url & "'; a.download = 'My file.pdf'; document.body.appendChild(a); a.click();"
Below is the full code:
Public Sub browser_open()
Set driver = New ChromeDriver
driver.SetPreference "download.prompt_for_download", False
driver.AddArgument "--kiosk-printing"
driver.Start "chrome"
driver.Get "https://data2.manualslib.com/pdf3/53/5221/522008-haier/washing_machine.pdf?b76112ef24159605ca8df71689bce0a7"
driver.ExecuteScript "var a = document.createElement('a'); a.href = '" & driver.url & "'; a.download = 'My file.pdf'; document.body.appendChild(a); a.click();"
Note!
It may take a few seconds for the download to begin.
Note!
To download multiple files you need to confirm this in the popup window, I do not know how to do this programmatically.

Open Chrome with options

With this script I open Chrome in Selenium VBA
Public Sub browser_open()
Set bot = New ChromeDriver
bot.Start "chrome", "http://some_site"
bot.get "/"
End Sub
Is it possible to open Chrome using the options --kiosk-printing, so that printing takes place without dialogue?

Error with Chrome profile selection with Selenium

I'd like to open a new Chromedriver session with a selected profile "Profile 1". But I also want to keep other Chrome window/session open (which has the default profile).
I tried this piece of code, it opens the new Chrome windows correctly and with "Profile 1" as I want.
But I then get following error from VBA.
Private Sub Use_Chrome_With_Custom_profile_name()
' Profiles folder : %APPDATA%\Google\Chrome\Profiles
' Note that with Chrome the profile is always persistant
Dim driver As New ChromeDriver
driver.AddArgument "--user-data-dir=C:\Users\user1\AppData\Local\Google\Chrome\User Data"
driver.AddArgument "--profile-directory=Profile 1"
driver.Get "https://www.google.co.uk"
driver.Quit
End Sub
If I skip (commenting it) this line then Chrome does'nt open with Profile 1:
'driver.AddArgument "--user-data-dir=C:\Users\user1\AppData\Local\Google\Chrome\User Data"
Try this:
Dim driver As New WebDriver
Const URL = "https://www.google.co.uk"
Const JS_PROFILE As String = _
"C:\Users\user1\AppData\Local\Google\Chrome\User Data\Profile 1"
Set driver = New ChromeDriver
With driver
.SetProfile JS_PROFILE, True
.Get URL
End With

Can't set chrome preferences in vba selenium

I've created a script in vba in combination with selenium to parse the first headline from this webpage. Most of the times my script throws this error timeout or this error Run-time error 21; Application defined or Object defined error while sometimes it works flawlessly. As the page takes too much time to load it's content, I suppose I'm having one of the side effect of a slow loading page, so I wish to disable images from that page.
I've tried with:
Sub TestSelenium()
Const URL$ = "https://www.marketscreener.com/"
Dim driver As Object, post As Object
Set driver = New ChromeDriver
driver.get URL
Set post = driver.FindElementByCss(".une_title")
MsgBox post.Text
driver.Quit
End Sub
When I go for python selenium binding, I can use this option to disable images:
option = webdriver.ChromeOptions()
chrome_prefs = {}
option.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"images": 2}
chrome_prefs["profile.managed_default_content_settings"] = {"images": 2}
driver = webdriver.Chrome(options=option)
I know there are options to set different preferences in vba but in case of disabling images I can't find any proper way to set them:
driver.SetPreference
driver.AddArgument
How can I set chrome preferences in vba selenium to let the page load quickly without images?
To disable images from that page, this is how you can set the preference within vba selenium bindings:
driver.SetPreference "profile.managed_default_content_settings.images", 2
Your script looks like the following when you implement the above suggestion:
Sub TestSelenium()
Const URL$ = "https://www.marketscreener.com/"
Dim driver As Object, post As Object
Set driver = New ChromeDriver
driver.SetPreference "profile.managed_default_content_settings.images", 2
driver.get URL
Set post = driver.FindElementByCss(".une_title")
MsgBox post.Text
Stop
driver.Quit
End Sub
You could always try running it headless? That should remove any delay associated with image loading.
driver.AddArgument "--headless"

vb.net open link in new IE11 tab (instead of window)

I need to force my application to open few links (there are few buttons - one link under one button) in IE11 (regardless if it's set as default browser or not).
I tried multiple ways, but the result is always the same like with:
Process.Start("iexplore.exe", address)
It works perfectly for Firefox, or Chrome, but for IE - it always opens new window for each link. Regardless IE is already opened or not.
Few years ago I wrote similar thing which worked with IE7 I guess...:
Imports SHDocVw
Dim internetExplorerInstances As New ShellWindows()
Dim foundIE As Boolean = False
foundIE = False
For Each ie As InternetExplorer In internetExplorerInstances
If ie.Name = "internet explorer" Then
ie.Navigate(address, &H800)
foundIE = True
Exit For
End If
Next
If Not foundIE Then
With oPro
.StartInfo.UseShellExecute = True
.StartInfo.Arguments = address
.StartInfo.FileName = "iexplore"
.Start()
End With
End If
But today, with IE 11 it doesn't work.... I mean it still opens URLs in new windows.
Do you have any ideas how to programatically force IE11 to open link in new Tab, not in new window?
Generally above code is OK except one detail - it should be:
If IE.Name = "Internet Explorer" Then... it is case sensitive.
Finally my code looks like this:
Imports SHDocVw
Const openInTab As Object = &H800
Dim internetExplorerInstances As New ShellWindows()
Public Function IsProcessOpen(ByVal name As String) As Boolean
For Each clsProcess As Process In Process.GetProcesses
If clsProcess.ProcessName.Contains(name) Then
Return True
End If
Next
Return False
End Function
(...)
And call:
If IsProcessOpen("iexplore") Then
For Each IE As InternetExplorer In internetExplorerInstances
If IE.Name = "Internet Explorer" Then
IE.Navigate2(address, openInTab)
Exit For
End If
Next
Else
Process.Start("iexplore", address)
End If
And it works :)