0

I want to zoom to a pdf that was uploaded to google. I try changing the 100% to 200% or other percentage, but the problem I have is that they are not buttons, but icons.

enter image description here

How do I zoom to the pdf?

Spvda
  • 23
  • 1
  • 8
  • have you tried to use keys for zooming? `driver.send_keys(Keys.chord(Keys.CONTROL, Keys.ADD));` – shiny Aug 05 '21 at 03:50
  • Nop. Could you give me an example of how to do it (according to the html that I show)? I am something new in selenium – Spvda Aug 05 '21 at 04:10
  • sure. just import the keys with `from selenium.webdriver.common.keys import Keys` execute the controll key + add key with `driver.send_keys(Keys.CONTROL + Keys.ADD)` when you have opened the pdf in selenium – shiny Aug 05 '21 at 04:42
  • It does not work :P – Spvda Aug 05 '21 at 05:05
  • let me do some testing – shiny Aug 05 '21 at 16:36

1 Answers1

0

This sis a bit hacky but i could not find a better solution for this:

what it does is to set the zoom level of the whole browser. you can check the zoom levels in the browser settings. e.g. 1.25 or 1.5.

def set_zoom(zoom = 1):
        current_tab_index = driver.window_handles.index(driver.current_window_handle)
        driver.execute_script("window.open('');")
        driver.switch_to.window(driver.window_handles[-1])
        driver.get('chrome://settings/')
        print(current_tab_index)
    
    def expand_shadow_element(element):
        shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
        return shadow_root

    # process to set zoom level of browser
    root1=driver.find_element_by_xpath("*//settings-ui")
    shadow_root1 = expand_shadow_element(root1)
    container= shadow_root1.find_element_by_id("container")

    root2= container.find_element_by_css_selector("settings-main")
    shadow_root2 = expand_shadow_element(root2)

    root3=shadow_root2.find_element_by_css_selector("settings-basic-page")

    shadow_root3 = expand_shadow_element(root3)
    basic_page = shadow_root3.find_element_by_id("basicPage")

    settings_section= basic_page.find_element_by_xpath(".//settings-section[@section='appearance']")

    root4= settings_section.find_element_by_css_selector("settings-appearance-page")
    shadow_root4=expand_shadow_element(root4)

    settings_animated_pages= shadow_root4.find_element_by_id("pages")
    zoom_select = settings_animated_pages.find_element_by_id("zoomLevel")
    zoom_select.find_element_by_css_selector(f"option[value='{zoom}']").click()

    driver.close()
    driver.switch_to.window(driver.window_handles[current_tab_index])
    driver.refresh()

code inspired by this answer here: https://stackoverflow.com/a/47086145/11699898

shiny
  • 658
  • 5
  • 8