I have been unable to run Edge in headless mode using Selenium with VBA in Excel - would someone be able to help me out with this? I can use the exact same code below using the ChromeDriver and it works as expected (i.e. - browser is hidden).
Here is a snippet of the code I am using...
sURL = "http://www.google.com"
Dim obj As Selenium.EdgeDriver
Set obj = New Selenium.EdgeDriver
obj.AddArgument "--headless" <-- the code runs fine but the Edge Brower is still visible
obj.Start
'Open URL in Edge Browser
obj.Get sURL
Thanks -
Jen
Dim obj As Selenium.EdgeDriver Set obj = New Selenium.EdgeDriver
obj.SetCapability "ms:edgeOptions", _ "{" & _ """args"":[""headless""]" & _ "}"
obj.Start
'https://gist.github.com/yas78/2a563995fde04c48260cebfe3aae371b
Related
I have a VBA macro that opens IE and navigates to a webpage and executes a javascript function on the page. This javascript function accepts a SQL command as an argument that is sent to a database and an object is returned.
This works for most commands, but sometimes the command is very long and IE ends up freezing/crashing with the parentwindow.execscript or parentwindow.eval. I am not sure if the issue is related to the length of the string or some other syntax error in the command, but I am able to run the command successfully manually in console and a valid object is returned in a couple seconds.
Is there some other way to do this through XMLHTTP or maybe some more stable method to call this command through the browser? Example code of what I have is below:
dim returnedQuery as variant
dim sqlquery as string
sqlquery = VeryLongPieceofCodeToQueryDatabase
dim ieapp as object
set ieapp = new internetexplorermedium
ieapp.navigate url
call busy(ieapp)
Set returnedQuery = ieapp.document.parentWindow.execScript("javascriptfunction(" & sqlquery & ")")
If XMLHTTP is more reliable, would someone be able to direct me to where I can read up about it?
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.
Here is my requirement which I intend to implement. There is one window application showing some icons which take me to different web sites. When I click on one of the icons, it should open an IE window and append a customized name to it.
So, before opening any website after I click on website icon, I want to check if there is already an IE window open with that customized name, if yes, bring that already opened window to the foreground. If not, open a new IE window.
I have checked various questions posted which are related to what I am looking to achieve, but am somehow not able to get it right. Below is my attempt.
For Each e In shellWins
If InStr(1, e.GetProperty("IEWindowName"), namedWindow, CompareMethod.Text) <> 0 Then
hWnd = e.HWND
myIE = e
End If
Next
If hWnd == -1
Dim p As New Process
Dim psi As New ProcessStartInfo(IEPath, webSiteURL)
p.StartInfo = psi 'Trying to open a new IE window
p.Start()
For Each ie In shellWins
If ie.hwdn = p.MainWindowHandle Then
ie.PutProperty("IEWindowName", namedWindow)
End If
Next
End if
Else
myIE.BringToForeground()
This sometime works and sometimes does not. Is there any better way to do it?
It doesn't work in which situation? Does there any error throw when it doesn't work? If there is, please tell us the detailed error information and in which line it occurs.
Besides, you could try to compare the url to check if the website is already open in IE like this:
Sub Main()
Dim shellWins As SHDocVw.ShellWindows
Dim explorer As SHDocVw.InternetExplorer
shellWins = New SHDocVw.ShellWindows
Dim SQuery As String = "https://www.example.com/"
For Each explorer In shellWins
If explorer.Application.Name = "Internet Explorer" And explorer.LocationURL.Contains(SQuery) Then
explorer.BringToForeground()
End If
Next
shellWins = Nothing
explorer = Nothing
End Sub
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"
I need to open two links in same tab in Google chrome browser. Below is the code I have tried, But I'm getting "File not found" error in line Browser = Shell(Chromepath & "-url URL1"). After opening the first URL(a login page) then it should navigates to second URL in same tab.
Sub Defects()
Dim ExtractliveApp As Variant
Dim Browser As Variant
Dim URL As Variant
URL1 = "go/ExtractliveApp"
URL2 = "https://Extract.live.com/SelfServiceExtracts"
Chromepath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Browser = Shell(Chromepath & "-url URL1")
Browser = Shell(Chromepath & "-url URL2")
End Sub
You have a typo in your code:
Browser = Shell(Chromepath & "-url URL1")
Browser = Shell(Chromepath & "-url URL2")
Should be
Browser = Shell(Chromepath & " -url " & URL1)
Browser = Shell(Chromepath & " -url " & URL2)
You need a space after the Chromepath as otherwise the Shell is looking for chrome.exe-url which it can't find (hence the error). I also fixed the use of your URL variables as they will need to be outside the quotes so that they are referenced correctly. Leaving them inside will cause them to be treated literally and Chrome will attempt to browse to URL1 rather than your link.