How to zoom out of page using python selenium

孤街醉人 提交于 2020-06-01 02:41:25

问题


I found this for java :-

WebElement html = driver.findElement(By.tagName("html"));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));

But how to do this using Python? I want to zoom out one level after get requests.


回答1:


You can do something similar where you specify the zoom levels as below:

For example, if my body has a <div id='values'>, I could zoom the div using

from selenium import webdriver

def main():
    browser = webdriver.Chrome()
    browser.set_window_size(1000, 1000)
    browser.get("http://yoursite.com")
    browser.execute_script("$('#values').css('zoom', 5);")

if __name__ == '__main__':
    main()

Or Simply try:

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



回答2:


This works for IE

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Ie(executable_path=path_to_driver, capabilities={'ignoreZoomSetting':True})
driver.get(url)
driver.find_element_by_tag_name('html').send_keys(Keys.CONTROL, '0')



回答3:


I looked all over for a solution to zoom out using selenium too, the documentation mentions nothing. Luckily the driver for Firefox (geckodriver) have this on one of their Github issues

I've made a shorthand brief of how to zoom out (and in), that will hopefully make the most sense (or at least that did for me)

#I'm sure this will be interchangeable with the Chrome driver too
 driver = webdriver.Firefox()
#Set the focus to the browser rather than the web content
 driver.set_context("chrome")
#Create a var of the window
 win = driver.find_element_by_tag_name("window")
#Send the key combination to the window itself rather than the web content to zoom out
#(change the "-" to "+" if you want to zoom in)
 win.send_keys(Keys.CONTROL + "-")
#Set the focus back to content to re-engage with page elements
 driver.set_context("content")


来源:https://stackoverflow.com/questions/32135085/how-to-zoom-out-of-page-using-python-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!