【发布时间】:2021-04-25 09:39:22
【问题描述】:
我为 java 找到了这个:-
WebElement html = driver.findElement(By.tagName("html"));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
但是如何使用 Python 做到这一点?我想在获取请求后缩小一级。
【问题讨论】:
标签: python selenium selenium-webdriver
我为 java 找到了这个:-
WebElement html = driver.findElement(By.tagName("html"));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
但是如何使用 Python 做到这一点?我想在获取请求后缩小一级。
【问题讨论】:
标签: python selenium selenium-webdriver
您可以在指定缩放级别时执行类似的操作,如下所示:
例如,如果我的body 有一个<div id='values'>,我可以使用缩放 div
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 %'")
【讨论】:
driver.execute_script("window.scrollTo(0, X)")' where X`是你要滚动的垂直位置。
这可以使用 selenium (-v 3.141.0) 完成 Python 的工作
driver = webdriver.Chrome(executable_path='...')
driver.get("www.stackoverflow.com")
driver.execute_script("document.body.style.zoom='50%'")
【讨论】:
我也到处寻找使用硒进行缩小的解决方案,文档没有提及。幸运的是,Firefox 的驱动程序(geckodriver)在他们的 Github 之一上有这个 issues
我已经简要介绍了如何缩小(和缩小),希望这将是最有意义的(或者至少对我来说是这样)
#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")
【讨论】:
driver.set_context("chrome")是什么意思
这适用于 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')
【讨论】: