1

I want to open a chrome window using the webbrowser module in Python. What I want to do next is to make the program resize the window. I've seen people do it with the selenium module, but it seemed way too complex for me since I have no experience with selenium. In addition, selenium webdriver doesn't work for me. Can someone help me resize a chrome window without the use of selenium?

Michael Butscher
  • 10,028
  • 4
  • 24
  • 25
Roni
  • 597
  • 5
  • 21
  • You have to download the Chromedriver binary and put it somewhere in your PATH first. All that is explained in the Selenium documentation. – MattDMo Aug 16 '21 at 18:26
  • @MattDMo, thanks! But is it possible to resize a chrome window without the use of selenium? – Roni Aug 16 '21 at 18:28
  • yes, you can also use `pyautogui` tho I can't guarantee that it will be easier – Matiiss Aug 16 '21 at 18:28
  • also why exactly do you need to resize the chrome window automatically? what is the issue you are trying to solve? – Matiiss Aug 16 '21 at 18:31
  • @Matiiss, how exactly do you use `pyautogui` to resize a chrome window? Please show me an example – Roni Aug 16 '21 at 18:31
  • @Roni first I need to see your efforts/attempts on this one then I can help solving a specific issue, resizing a window is not specific, because it can be done in multiple ways like with `selenium` or `pyautogui`, both are very different – Matiiss Aug 16 '21 at 18:32
  • @Matiiss, I'm working on an html editor and want to display the output of the code in a chrome browser. But I want to make the chrome window smaller so users can see both the code and the browser – Roni Aug 16 '21 at 18:33
  • 1
    @Roni and the user can't resize the window themselves? even very popular editors don't do that. And what if the user doesn't want anything resized? (ok, that at least can be a setting) – Matiiss Aug 16 '21 at 18:34
  • @Matiiss, I just want both the code and the output to be displayed on the screen. I don't want the user to have to do it themselves – Roni Aug 16 '21 at 18:35
  • currently there seems to be no easy way to do it with `pyautogui`, the best I managed was to minimize a window, resizing didn't happen tho they have such functionality [planned](https://pyautogui.readthedocs.io/en/latest/roadmap.html), you can try either [PyGetWindow](https://github.com/asweigart/PyGetWindow) or [pywinauto](https://github.com/pywinauto/pywinauto) to achieve what you want, they don't seem to be completely complete libraries but maybe they help, also a related question: [Python pyautogui window handle](https://stackoverflow.com/questions/43785927/python-pyautogui-window-handle) – Matiiss Aug 16 '21 at 19:08

1 Answers1

0

I tried solving the problem with pyautogui as others suggested, but didn't find a way to solve my problem. I figured there was no easier way other than using selenium, so I decided to learn how to use it. I experimented with selenium and figured out how to resize and change the position of a chrome browser along with some other things. Code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome("C:\chromedriver.exe")
driver.get("https://www.python.org")
print(driver.title)
search_bar = driver.find_element_by_name("q")
search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)
print(driver.current_url)
driver.set_window_size(400, 600)
driver.set_window_position(300, 20, windowHandle ='current')
print(driver.get_window_size())
Roni
  • 597
  • 5
  • 21