簡體   English   中英

如何使用python selenium縮小頁面

[英]How to zoom out of page using python selenium

我為 Java 找到了這個:-

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

但是如何使用 Python 做到這一點呢? 我想在獲取請求后縮小一級。

您可以在指定縮放級別的地方執行類似的操作,如下所示:

例如,如果我的body有一個<div id='values'> ,我可以使用

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()

或者簡單地嘗試:

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

我也四處尋找使用 selenium 縮小的解決方案,文檔沒有提到任何內容。 幸運的是 Firefox (geckodriver) 的驅動程序在他們的 Github問題之一上有這個

我已經簡要介紹了如何縮小(和縮小),這有望最有意義(或至少對我而言如此)

#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")

這適用於 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')

這用 selenium (-v 3.141.0) 完成了 Python 的工作

driver = webdriver.Chrome(executable_path='...')
driver.get("www.stackoverflow.com")
driver.execute_script("document.body.style.zoom='50%'")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM