[Python] How to zoom out with Selenium in Python?

Discussão em 'Python' iniciado por Stack, Março 4, 2025.

  1. Stack

    Stack Membro Participativo

    An element exists but I cannot see it on a web page. So I cannot click on it. To access the element, I have to zoom out of the page.

    When I use code

    browser.execute_script("document.body.style.zoom='40%'")


    the page zooms out well. But the element I wanted to reach is stuck at the top of the page and not accessible for clicking anyway.

    I expected to zoom out the page by pressing a combination of keys Ctrl+'-' with Selenium but failed to get any result. I tried this code

    import time
    from selenium import webdriver
    from selenium.webdriver import Keys
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.by import By

    with webdriver.Chrome() as browser:
    browser.get('https://some_address/index.html')
    time.sleep(1)
    element=browser.find_element(By.ID, 'some_text')
    actions=ActionChains(browser)
    actions.key_down(Keys.CONTROL,element) \
    .key_down(Keys.SUBTRACT,element).key_up(Keys.SUBTRACT,element) \
    .key_down(Keys.SUBTRACT,element).key_up(Keys.SUBTRACT,element) \
    .key_up(Keys.CONTROL,element).perform()


    but zoom out does not work.

    Continue reading...

Compartilhe esta Página