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.)
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