stevepryde/thirtyfour

How to disable loading of images? (Google Chrome in my case, if there is no browser agnostic way)

Closed this issue · 5 comments

Hello,

I am currently rewriting a project of mine I did in Python a long time ago.
For this I decided to go the route of Rust and using thirtyfour to control Google Chrome.

With Python I was able to disable the loading of images in the chromedriver via Chrome Options, but I am not able to do it under Rust - I simply am not able to find anything about it, or be lucky with my tries setting differently formatted options so far.
For Python I used experimental options which can be seen here.
I also didn't find any function on the capabilities struct analogous to set_headless, which would disable images - if that makes sense at all, I don't know if one can implement that browser agnostic in the first place.
Could you maybe lend a hand and help me out here please?

Use-case: it saves a lot of data when you are on a budget, and it speeds up loading websites by a lot since not as much data has to be transferred.

Kind regards

For headless you need to use ChromeCapabilities::set_headless(). There is no browser-agnostic way to do this.

As for the experimental options, if you know what the resulting options dict looks like in Python you could probably get the right key/value pairs into ChromeCapabilities but it's often quite difficult to figure out what it wants. I'll see if I can either find the Chrome documentation on this or alternatively it may be possible to just reverse-engineer what the python lib does and copy that.

Ok so it looks like the experimental options are just regular chrome options.

You should be able to use the following:

    let mut caps = DesiredCapabilities::chrome();
    caps.set_headless()?;
    caps.add_chrome_option(
        "prefs",
        serde_json::json!({
            "profile.default_content_settings": {
                "images": 2
            },
            "profile.managed_default_content_settings": {
                "images": 2
            }
        }),
    )?;
    let driver = WebDriver::new("http://localhost:4444", &caps).await?;

I've also added this under examples/ so that others can find it easier

Hi Steve,

I just tested it and it works like a charm.
Thank you so much for helping me out!

About headless: yes, I already knew it exists and how to activate it, but if I am not mistaken, headless mode still loads images, even if it doesn't render anything at all - haven't tested it though.
When I disable loading of images, they stop appearing in the network monitor of the developer tools, and that's what I wanted to achieve.

Kind regards

Yes you're right - headless will still load images but nothing will be rendered. Glad I could help 👍