1

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.

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

This code is breaking the Xpath access: driver.execute_script("document.body.style.zoom='150%'")

I observed error when I am using pyautogui with 'headless'. options.add_argument("--headless=new")

Solution: when using

options.add_argument("--headless=new")

   options.add_argument("window-size=1400,600")
   browser.set_window_size(1080, 740)
   options.add_argument("--headless=new")

Solution link:

Selenium Firefox headless returns different results

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.