4

I am trying to set google chrome zoom to 80%.


from selenium import webdriver

class bot:

    def __init__(self):
        self.driver = webdriver.Chrome("chromedriver path here")

    def zoomOut(self):
        driver = self.driver
        driver.get('chrome://settings/')
        driver.find_element_by_xpath('//*[@id="zoomLevel"]').sendKeys('80').sendKeys(Keys.ENTER)

if __name__ == "__main__":
    bot = bot()
    bot.zoomOut()

Would appreciate any help on how to solve this issue.

Update: I get an error on finding the xpath to be able to set the zoom value.

A D
  • 135
  • 8
  • Does this answer your question? [How to zoom out of page using python selenium](https://stackoverflow.com/questions/32135085/how-to-zoom-out-of-page-using-python-selenium) – Yaakov Bressler Apr 03 '20 at 03:11
  • I tried those ways too, but they did not work for me. I also tried: driver.execute_script("document.body.style.zoom='zoom %'") – A D Apr 03 '20 at 03:23
  • Do you get an error? – Robert Kearns Apr 03 '20 at 03:54
  • For my attempt in my question I get an error when I try to find by xpath to set the zoom value. When I tried: driver.execute_script("document.body.style.zoom='zoom %'") as another question suggested on stack overflow, no changes were made to the zoom value. – A D Apr 03 '20 at 03:58

1 Answers1

2

This is an interesting question. This is difficult because the settings elements are very nested in a bunch of shadow roots (a "sub dom", similar to an iFrame). It is nested very deep, and is a bit of a pain to get to. Here is some JS code that gets us there:

var selectBox = document.querySelector("settings-ui").shadowRoot.querySelector("#main").shadowRoot.querySelector("settings-basic-page").shadowRoot.querySelector("settings-appearance-page").shadowRoot.querySelector("#zoomLevel");

var changeEvent = new Event("change");

selectBox.value = arguments[0];
selectBox.dispatchEvent(changeEvent); // Trigger change event to change the zoom

We can then put this in a python method to change the zoom.

class Bot:
    def change_zoom(self, new_zoom):
        """
        :param new_zoom: zoom level as a percentage
        """
        change_js = """
        var selectBox = document.querySelector("settings-ui").shadowRoot.querySelector("#main").shadowRoot.querySelector("settings-basic-page").shadowRoot.querySelector("settings-appearance-page").shadowRoot.querySelector("#zoomLevel");

        var changeEvent = new Event("change");

        selectBox.value = arguments[0];
        selectBox.dispatchEvent(changeEvent);
        """

        self.driver.get("chrome://settings/")
        new_zoom = round(new_zoom / 100, 2)
        self.driver.execute_script(change_js, new_zoom)

I am very sure there is an easier way to access these deep nested elements, but this works well for me.

Robert Kearns
  • 1,631
  • 1
  • 8
  • 15
  • Really appreciate your help and explanation. It seems that after using this zoom method, the rest of my web scraping code is not functioning as intended. Specifically, elements are no longer being found with xpaths that work when I do not call the zoom method. Any ideas on why? – A D Apr 03 '20 at 05:42
  • 1
    Tough to say, especially if your xPaths are not verbose (are very specific). Changing the zoom of the page could trigger some handlers that alter the structure of the page. What do your xPaths look like? Are they typically pretty lengthy? – Robert Kearns Apr 03 '20 at 05:48
  • example of a section of my code that navigates to and Instagram profile and clicks on the first post: driver.get("https://www.instagram.com/zuck/")time.sleep(2)WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//body//div[contains(@class,'_2z6nI')]//div//div//div[1]//div[1]//a[1]//div[1]//div[2]"))).click() Sorry this is messy, but yes some xpaths are quite long. – A D Apr 03 '20 at 05:51
  • 1
    Oh yeah, that is very fragile. All it takes is the page structure to change slightly and your whole query falls apart. What does the target element look like? – Robert Kearns Apr 03 '20 at 05:52
  • :( the target element if u navigate to "instagram.com/zuck/" for instance is the most recent post. – A D Apr 03 '20 at 05:54
  • 1
    ```article img[srcset]``` returns all post image in a list (that is a css selector). That will be much more flexible then using xPaths. – Robert Kearns Apr 03 '20 at 06:29