0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Python】Seleniumでブラウザ内部の(クライアント)ウィンドウサイズを設定する方法

Posted at

やりたいこと

selenium(ブラウザ自動化フレームワーク)で、クライアントウィンドウサイズの設定をしたい。

(タブやURLなどを含めた、「実際の」windowサイズを設定の設定は簡単にできます。ただ、実際のHTMLが表示される「内部の」windowサイズを指定するのが少しだけ面倒です)

コード

実際のウィンドウサイズと内部のウィンドウサイズの差分をとることで、外枠の大きさを得られます。
あとは設定したいサイズに外枠を足せば、設定するべき実際の画面サイズがわかります。

pythonで書きます
width = 600 # 設定したい内部の横幅(ClientWidth)
height = 400 # 設定したい内部の縦幅(ClientHeight)
driver = webdriver.Chrome(options=options) # 設定は省略

# 現在のwindowサイズをdriverから抜き出す
current_window_size = driver.get_window_size()

# htmlタグからクライアントのwindowサイズを抜き出す
html = driver.find_element_by_tag_name('html')
inner_width = int(html.get_attribute("clientWidth"))
inner_height = int(html.get_attribute("clientHeight"))

# 「設定したい内部の幅+外枠の幅」をwindowサイズにセット
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))

参考にした記事
https://stackoverflow.com/questions/36333708/how-to-set-browser-client-area-size-using-selenium-webdriver

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?