简体   繁体   English

Python:如何使用 selenium 缩小页面?

[英]Python: How to zoom out of a page using selenium?

I am trying to zoom out to 25% in my python selenium program,我试图在我的 python selenium 程序中缩小到 25%,

It should zoom out from this:它应该从这个缩小: 在此处输入图像描述

to this:对此: 在此处输入图像描述

As you can see the elements that should appear when scrolling down in the first image are all visible in the second image when zoomed out to 25%.正如您所看到的,在第一张图片中向下滚动时应该出现的元素在缩小到 25% 时在第二张图片中都是可见的。

I tried driver.execute_script("document.body.style.zoom='25%'") but that's how it zoomed out:我试过driver.execute_script("document.body.style.zoom='25%'")但它就是这样缩小的: 在此处输入图像描述

For some reason these solutions didn't do anything for me:出于某种原因,这些解决方案对我没有任何作用:

1- 1-

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

actions = ActionChains(driver)
actions.key_down(Keys.CONTROL).send_keys(Keys.SUBTRACT).key_up(Keys.CONTROL).perform()

2- 2-

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

zoomOut = ActionChains(driver)
zoomOut.key_down(Keys.CONTROL)
for i in range(7):
    print(i)
    zoomOut.send_keys("-")
zoomOut.key_up(Keys.CONTROL)
zoomOut.perform()

3- The solution driver.execute_script("$('id_body').css('zoom', 25);") in this question doesn't do anything in my program. 3-这个问题中的解决方案driver.execute_script("$('id_body').css('zoom', 25);")在我的程序中没有做任何事情

I tested the first option (second option same as this) and found that the code works great.我测试了第一个选项(第二个选项与此相同)并发现代码运行良好。 But it works only at the page elements level not at the window level or chrome itself.但它仅适用于页面元素级别,不适用于 window 级别或 chrome 本身。 You can see for yourself by running the code below.您可以通过运行下面的代码来亲眼看看。

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
# set chromodriver.exe path
driver = webdriver.Chrome(executable_path='C:\\chromedriver\\chromedriver108.exe')
# object of ActionChains
action = ActionChains(driver)
# launch URL
# driver.get("https://keyboard-test.space/")
driver.get("https://en.key-test.ru/")
# driver.get("https://keyboardtestt.com/")
action.key_down(Keys.CONTROL).send_keys(Keys.SUBTRACT).key_up(Keys.CONTROL).perform()
# wait user input
input()
# close browser
driver.close()

Then I browsed stackoverflow on related topics.然后我浏览了相关主题的stackoverflow。 In the Why are Selenium Chromedriver shortcuts not working?为什么 Selenium Chromedriver 快捷方式不起作用? @pcalkins says: @pcalkins 说:

The scope is limited to the DOM more or less. scope 或多或少受限于 DOM。 There used to be some support for it (not necessarily by design), but I think all the webdrivers/browsers dropped it.曾经有一些支持(不一定是设计的),但我认为所有的网络驱动程序/浏览器都放弃了它。

In the python selenium sendkey doesn't work in chrome @soundwave says:python selenium 中,sendkey 在 chrome 中不起作用 @soundwave 说:

Webdriver is designed to be used to drive the web-page, rather than to do browser specific actions. Webdriver 旨在用于驱动网页,而不是执行特定于浏览器的操作。 See here bugs.chromium.org请参阅此处bugs.chromium.org

"This is a limitation in the way we simulate keyboard input in ChromeDriver. Keys get sent directly to the render process, bypassing the browser process. So any keyboard shortcut handlers in the browser process will not be invoked" “这是我们在 ChromeDriver 中模拟键盘输入的方式的限制。键直接发送到渲染进程,绕过浏览器进程。因此浏览器进程中的任何键盘快捷键处理程序都不会被调用”

and in the " Selenium Chrome Driver send key press combinations to window " @AndrewRegan says:在“ Selenium Chrome Driver send key press combinations to window ”@AndrewRegan 中说:

The WebDriver spec is element-focussed, and doesn't define any method to send keys to the window, the screen, to browser chrome - only to elements. WebDriver 规范以元素为中心,并且没有定义任何方法来将键发送到 window、屏幕、浏览器 chrome - 仅发送到元素。

Use of the Selenium Actions class for Cmd-R works on my Mac in Firefox (45), but only when run in the foreground - and seemingly not at all in Chrome.使用 Selenium Actions class for Cmd-R 可以在我的 Mac 上运行 Firefox (45),但仅当在前台运行时 - 并且在 Chrome 中似乎根本没有。 Presumably this is down to differences in the implementations of the remote Keyboard implementation, which it's probably best not to rely upon.据推测,这是由于远程键盘实现的实现差异所致,最好不要依赖它。

So the answer is that the problem is from the website, to be able to see all the elements we should scroll down so that the rest of the data is loaded.所以答案是问题出在网站上,为了能够看到我们应该向下滚动的所有元素,以便加载数据的 rest。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM