Search code examples
python-3.xseleniumfirefoxzooming

How to zoom in a webpage using Python Selenium in FireFox


I am working on Ubuntu and writing a web automation script which can open a page and zoom in the page. I am using python selenium. Below is the code:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys


driver = webdriver.Firefox()
driver.get("https://www.wikipedia.org/")

driver.maximize_window()
time.sleep(2)

This opens the browser and opens the wikipedia page. Now I have to do the zoom in functionality. For this I tried below line of code:

driver.execute_script("document.body.style.zoom='150%'")

It didn't work. So I tried below line of code:

driver.execute_script('document.body.style.MozTransform = "scale(1.50)";')

driver.execute_script('document.body.style.MozTransformOrigin = "0 0";')

It kind of worked but the webpage height and width were disturbed and it looks like below:

enter image description here

It should look like below:

enter image description here

Can anyone please suggest some good working solution to zoom in a webpage using python selenium in FireFox.


Solution

  • I didn't find any working solution so what I did is, I used pyautogui to simulate the keys.

    pyautogui.keyDown('ctrl')
    pyautogui.press('+')
    pyautogui.keyUp('ctrl')
    

    This made the webpage zoom to 110%. You can run it again to make it to 120% and so on.

    Thanks