How to define windows size for Microsoft Edge with Selenium in Python?

2.1k views Asked by At

I am trying to write a python program that visits a site hosted by me for testing and development purposes, I would like to use Microsoft Edge browser only for this task but I can't seem to figure out how to run the script headless so that it would consume fewer resources, I did my research but Edge browser does not seem to have a headless option as far as I understand correct me if I am wrong please, so is there any way to size the browser window that pops up on my screen to zero dimensions, if so does it use fewer resources since there is nothing to render?

from  selenium import webdriver
from selenium.webdriver.common.keys import Keys
#from selenium.webdriver.edge.options import Options

# setting up headless option for faster execution
#options = Options()
#options.headless = True


engine = webdriver.Edge()
#engine = webdriver.Edge(executable_path="path/to/executable")

engine.get('my_site_link_goes_here')


assert 'title' in engine.title


print ("Done")

when I run this script everything works as expected but I would like to make it close to headless as much as possible

ps: I can only use Edge browser for testing

2

There are 2 answers

2
Vemula Dheeraj On BEST ANSWER

Selenium webdriver does not provide any method for minimizing the browser, there is no such direct method. You need to use resize method to minimize the browser.

Dimension d = new Dimension(300,1080);
#Resize current window to the set dimension
driver.manage().window().setSize(d);
0
Super Kai - Kazuya Ito On

You can set window size for Microsoft Edge as shown below. *-window-size and window-size also work:

from selenium import webdriver

options = webdriver.EdgeOptions()
options.add_argument("--window-size=1024,768")
driver = webdriver.Edge(options=options)

And, you can also set window size for Microsoft Edge with the code below according to the doc:

from selenium import webdriver

driver = webdriver.Edge()
driver.set_window_size(1024, 768)

*My answer explains it more.