Giter Site home page Giter Site logo

Set browser width/height about chromedp HOT 8 CLOSED

clanstyles avatar clanstyles commented on May 14, 2024
Set browser width/height

from chromedp.

Comments (8)

kenshaw avatar kenshaw commented on May 14, 2024

You need to set the width/height using a command line option. Unfortunately, this is dependent on the operating system (and I'm not sure if this is going to change for the headless version, either).

Basically, you pass a runner option to chromedp.New():

ctxt, cancel := context.WithCancel(context.Background());
defer cancel()
c, err := chromedp.New(ctxt, chromedp.WithRunnerOptions(runner.WindowSize(width, height)))

from chromedp.

kenshaw avatar kenshaw commented on May 14, 2024

BTW, in the future I do plan to be able to prove some way to resize the window dynamically, but the issue with that is that currently Chrome (as far as I can tell) does not support that via the Remote Debugging Protocol, and I would need to write cross-platform support to resize the window via either system calls or via some other mechanism, and I just don't have the bandwidth at the moment to do the research needed. Also, as this is geared mostly towards running remote chrome instances, it wouldn't work when you're running chrome that way.

from chromedp.

clanstyles avatar clanstyles commented on May 14, 2024

There has to be some way, how else would you emulate mobile?

I found

metrics := emulation.SetDeviceMetricsOverride(1024, 4068, 0, false, false)

// set the device metrics
if err := c.Run(ctxt, metrics); err != nil {
	log.Fatal(err)
}

Which didn't set the actual device though. I've also noticed if you try to issue a "Click" on a button that's not within the window's view, it can't click it and dies.

from chromedp.

kenshaw avatar kenshaw commented on May 14, 2024

The rendering and events of Chrome are completely dependent on the items being in view, which, yes, I know, is a giant pain. You need to scroll to the element first (ie, via javascript) before applying an action against something. I am in the process of overhauling the existing actions to take this into account. With headless chrome, this may not be necessary. I am definitely not an expert with the Chrome API yet, but I also had tried the emulation domain to override settings, and it did not do what I had hoped.

from chromedp.

clanstyles avatar clanstyles commented on May 14, 2024

How would you go about scrolling to the element?

from chromedp.

clanstyles avatar clanstyles commented on May 14, 2024

I was able to get further with:

	vp := emulation.ForceViewport(1920, 1080, 1.0)
	metrics := emulation.SetDeviceMetricsOverride(1920, 1080, 1, false, false)

	if err = c.Run(ctxt, vp); err != nil {
		log.Fatal(err)
	}

	if err = c.Run(ctxt, metrics); err != nil {
		log.Fatal(err)
	}

It seems like it's able to interact with buttons that would be out of the window, but it's now within the view port. The downfall is that the page is gray. I was able to get the page to render by pressing "F12 (dev tools)".

from chromedp.

clanstyles avatar clanstyles commented on May 14, 2024

https://groups.google.com/a/chromium.org/forum/#!topic/headless-dev/qcvOjVUZ-WQ

This seems to talk about it. From what I can tell, the buffer thinks it's that size, but the window might not actually be the size (that's why we see gray). So in theory, this is working, it's just not viewable.

from chromedp.

kenshaw avatar kenshaw commented on May 14, 2024

I have a big fix rollup that I'll be pushing either late today or early tomorrow, that should fix the issues for elements that are off screen.

Basically, a "Click" action actually becomes a composition of the following:

  1. Get dimensions (DOM.getBoxModel) of content
  2. window.scrollTo() to the element's location via Runtime.evaluate
  3. DOM.focus()
  4. Input.dispatchMouseEvent with button down / up

From my simple testing so far, this seems to work 100% of the time -- I'm not going to try to claim it's 100% perfect, but I haven't found any failure scenarios yet.

from chromedp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.