相关疑难解决方法(0)

如何在Selenium(Python)中将打开的页面保存为pdf

已经尝试了我可以在 Internet 上找到的所有解决方案,以便能够打印在 Python 中在 Selenium 中打开的页面。然而,当打印弹出窗口出现时,一两秒钟后它就会消失,没有保存 PDF。

这是正在尝试的代码。基于此处的代码 - /sf/answers/3062649061/

使用 Mojave 10.14.5 在 Mac 上编码。

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import WebDriverException
import time
import json

options = Options()
appState = {
    "recentDestinations": [
        {
            "id": "Save as PDF",
            "origin": "local"
        }
    ],
    "selectedDestinationId": "Save as PDF", …
Run Code Online (Sandbox Code Playgroud)

python selenium python-3.x selenium-chromedriver selenium-webdriver

14
推荐指数
3
解决办法
2万
查看次数

当它不是无头时,Selenium 不会截取整个网站的屏幕截图

免责声明:我知道,已经有一个类似的问题了,但是没有一个答案适用于无头浏览器,所以我决定制作一个更详细的问题(我提到的问题:用带有 chromedriver 的 Selenium Python

大家好。

我偶然发现了一个看起来很简单但很难解决的问题。我需要在显示器上截取非无头浏览器的屏幕截图,即 1920x1080(稍后会很重要),这将截取整个网页的屏幕截图,而不仅仅是您当前可以看到的部分。

我尝试过什么:

import os
import time

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument("--start-maximized")
chromedriver = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'chromedriver.exe')
chrome = webdriver.Chrome(chromedriver, options=chrome_options)

url = 'https://stackoverflow.com/'

chrome.get(url)
time.sleep(2)

total_height = chrome.execute_script("return document.body.parentNode.scrollHeight") + 1000

chrome.set_window_size(1920, total_height)

time.sleep(2)
chrome.save_screenshot("screenshot1.png")
chrome.quit()
Run Code Online (Sandbox Code Playgroud)

^ 这个,无头工作得很好,不幸的是,当我删除该--headless选项时,selenium 会尝试调整自身大小,但由于它试图调整到1080height显示器)上方的大小,因此它立即调整为1080导致屏幕截图的大小1920x1080。我需要的“理论”方式是让硒headless仅在截取屏幕截图时暂时运行(不幸的是,据我所知,这是不可能的)。

其他在浏览器非无头时不起作用的常用方法:

el = driver.find_element_by_tag_name('body')
el.screenshot(path)
Run Code Online (Sandbox Code Playgroud)
original_size = driver.get_window_size()
required_width = driver.execute_script('return document.body.parentNode.scrollWidth')
required_height = driver.execute_script('return document.body.parentNode.scrollHeight')
driver.set_window_size(required_width, …
Run Code Online (Sandbox Code Playgroud)

python selenium google-chrome selenium-chromedriver selenium-webdriver

3
推荐指数
1
解决办法
3050
查看次数

使用python硒和Firefox或Chrome浏览器获取整个页面的截图

这篇帖子与此相关:

Python Selenium屏幕截图无法获取整个页面

PhantomsJS的解决方案似乎正在起作用:

driver = webdriver.PhantomJS()    
driver.maximize_window()
driver.get('http://www.angelfire.com/super/badwebs/')  
scheight = .1
while scheight < 9.9:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight/%s);" % scheight)
    scheight += .01        
driver.save_screenshot('angelfire_phantomjs.png')
Run Code Online (Sandbox Code Playgroud)

但是,该解决方案来自2014年,同时不建议使用PhantomJS。我正在收到此错误消息:

...
UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
Run Code Online (Sandbox Code Playgroud)

如果我尝试像这样无头地适应Firefox:

from selenium import webdriver

firefox_options = webdriver.FirefoxOptions()
firefox_options.set_headless() 
firefox_driver = webdriver.Firefox(firefox_options=firefox_options)

firefox_driver.get('http://www.angelfire.com/super/badwebs/')  
scheight = .1
while scheight < 9.9:
    firefox_driver.execute_script("window.scrollTo(0, document.body.scrollHeight/%s);" % scheight)
    scheight += …
Run Code Online (Sandbox Code Playgroud)

python firefox selenium google-chrome

0
推荐指数
1
解决办法
3407
查看次数