[Python] How to set the (client) window size inside the browser with Selenium

Thing you want to do

I want to set the client window size with selenium (browser automation framework).

(It's easy to set the "real" window size, including tabs, URLs, etc. However, it's a bit tedious to specify the "internal" window size where the actual HTML is displayed.)

code

The size of the outer frame can be obtained by taking the difference between the actual window size and the internal window size. After that, if you add the outer frame to the size you want to set, you can see the actual screen size to be set.

Written in python


width = 600 #Internal width you want to set(ClientWidth)
height = 400 #Internal height you want to set(ClientHeight)
driver = webdriver.Chrome(options=options) #Setting omitted

#Extract the current window size from the driver
current_window_size = driver.get_window_size()

#Extract the client window size from the html tag
html = driver.find_element_by_tag_name('html')
inner_width = int(html.get_attribute("clientWidth"))
inner_height = int(html.get_attribute("clientHeight"))

#"Internal width you want to set+Set "outer frame width" to window size
target_width = width + (current_window_size["width"] - inner_width)
target_height = height + (current_window_size["height"] - inner_height)
driver.set_window_rect(
    width=target_width,
    height=target_height)

print("Window size changed: [{}, {}]".format(target_width, target_height))

Referenced articles https://stackoverflow.com/questions/36333708/how-to-set-browser-client-area-size-using-selenium-webdriver

Recommended Posts

[Python] How to set the (client) window size inside the browser with Selenium
[Python] Automatically operate the browser with Selenium
[Python] How to specify the window display position and size of matplotlib
[Introduction to Python] How to iterate with the range function?
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Python] How to rewrite the table style with python-pptx [python-pptx]
How to switch to smartphone mode with Python + Selenium + Chrome
How to set the development environment for each project with VSCode + Python extension + Miniconda
I tried to simulate how the infection spreads with Python
How to scrape at speed per second with Python Selenium
How to get into the python development environment with Vagrant
[Introduction to Python] How to get data with the listdir function
Python: How to use async with
How to get the Python version
[Python] How to import the library
How to use FTP with Python
How to calculate date with python
[Introduction to Python] How to split a character string with the split function
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
[Python] Explains how to use the format function with an example
How to automatically install Chrome Driver for Chrome version with Python + Selenium + Chrome
How to send a request to the DMM (FANZA) API with python
[Blender] How to set shape_key with script
[Python] Set the graph range with matplotlib
How to set the server time to Japanese time
Display Python 3 in the browser with MAMP
How to work with BigQuery in Python
How to do portmanteau test with python
How to display python Japanese with lolipop
How to enter Japanese with Python curses
[Python] How to deal with module errors
The road to compiling to Python 3 with Thrift
How to install python3 with docker centos
[Python] What is a formal argument? How to set the initial value
How to crop the lower right part of the image with Python OpenCV
[Python] Explains how to use the range function with a concrete example
The 16th offline real-time how to write reference problem to solve with Python
How to get the date and time difference in seconds with python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Introduction to Python] How to write a character string with the format function
The 19th offline real-time how to write reference problem to solve with Python
The 15th offline real-time how to write problem was solved with python
How to upload with Heroku, Flask, Python, Git (4)
How to use the C library in Python
How to read a CSV file with Python 2/3
How to get started with the 2020 Python project (windows wsl and mac standardization)
How to specify the launch browser for JupyterLab 3.0.0
How to enjoy programming with Minecraft (Ruby, Python)
[REAPER] How to play with Reascript in Python
How to specify the NIC to scan with amazon-dash
How to open a web browser from python
How to do multi-core parallel processing with python
Strategy on how to monetize with Python Java
[Python] How to change the date format (display format)
How to set browser location in Headless Chrome
The easiest way to synthesize speech with python
Try to solve the man-machine chart with Python
[Python] How to draw multiple graphs with Matplotlib
Specify the Python executable to use with virtualenv
[Python] How to read excel file with pandas