MITHU
MITHU

Reputation: 164

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?

Upvotes: 1

Views: 3000

Answers (2)

SIM
SIM

Reputation: 22440

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

Upvotes: 1

QHarr
QHarr

Reputation: 84465

You could always try running it headless? That should remove any delay associated with image loading.

driver.AddArgument "--headless"

Upvotes: 0

Related Questions