selenium 如何使用PYTHO SELENCE缩小页面

ttvkxqim  于 2022-11-10  发布在  其他
关注(0)|答案(5)|浏览(154)

我为 java 找到了这个:-

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

但如何使用Python做到这一点呢?我想在GET请求之后缩小一个级别。

flvlnr44

flvlnr441#

在指定缩放级别时,可以执行类似的操作,如下所示:
例如,如果我的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 %'")
w51jfk4q

w51jfk4q2#

这将为使用Selify(-v 3.141.0)的Python完成此工作

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

8nuwlpux3#

我也在到处寻找使用Selify缩小的解决方案,但文档没有提到任何内容。幸运的是,Firefox的驱动程序(gecko驱动程序)在他们的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")
kulphzqa

kulphzqa4#

这适用于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')
y1aodyip

y1aodyip5#

嘿,伙计们,我对使用Firefox的所有解决方案都有问题,有些解决方案没有完全像预期的那样工作,所以在你们的帮助下,我创建了这个解决方案。

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

driver = webdriver.Firefox()

driver.implicitly_wait(5)

driver.get("https://stackoverflow.com/")

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

# for zoom out this will change to 90% if you need more copy and paste it again. Or you can change - to + for zoom in.

win.send_keys(Keys.CONTROL + "-")

# Set the focus back to content to re-engage with page elements

driver.set_context("content")

相关问题