![](/img/trans.png)
[英]clickAndHold is not working in Selenium webdriver (python)
[英]Selenium webdriver python element screenshot not working properly
我查閱了 Selenium python 文檔,它允許截取元素的屏幕截圖。 我嘗試了以下代碼,它適用於小頁面(打印時大約 3-4 個實際 A4 頁):
from selenium.webdriver import FirefoxOptions
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("browser.privatebrowsing.autostart", True)
# Configure options for Firefox webdriver
options = FirefoxOptions()
options.add_argument('--headless')
# Initialise Firefox webdriver
driver = webdriver.Firefox(firefox_profile=firefox_profile, options=options)
driver.maximize_window()
driver.get(url)
driver.find_element_by_tag_name("body").screenshot("career.png")
driver.close()
當我嘗試使用url="https://waitbutwhy.com/2020/03/my-morning.html"
時,它按預期提供了整個頁面的屏幕截圖。 但是當我嘗試使用url="https://waitbutwhy.com/2018/04/picking-career.html"
時,屏幕截圖中幾乎一半的頁面沒有呈現(圖片太大無法上傳到這里),即使“body”標簽確實在原始 HTML 中一直向下延伸。
我試過同時使用隱式和顯式等待(設置為 10 秒,這足以讓瀏覽器加載所有內容,包括評論和討論部分),但這並沒有提高屏幕截圖功能。 為了確保 selenium 實際上正確加載了網頁,我嘗試在沒有headless
標志的情況下加載,一旦網頁完全加載,我就運行了driver.find_element_by_tag_name("body").screenshot("career.png")
. 屏幕截圖又是半空白的。
看起來screenshot
方法可能有一些內存限制(雖然我找不到),或者screenshot
方法本身背后的邏輯是有缺陷的。 不過我想不通。 我只是想截取整個“body”元素的屏幕截圖(最好是在headless
環境中)。
你可以試試這個代碼,只是你需要使用命令pip install Selenium-Screenshot
從命令提示符安裝一個包
import time
from selenium import webdriver
from Screenshot import Screenshot_Clipping
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)
driver.get("https://waitbutwhy.com/2020/03/my-morning.html")
obj=Screenshot_Clipping.Screenshot()
img_loc=obj.full_Screenshot(driver, save_path=r'.', image_name='capture.png')
print(img_loc)
time.sleep(5)
driver.close()
結果/結果就像,你只需要放大保存的屏幕截圖
希望這對你有用!
Selenium-Screenshot 2.0 沒有 Screenshot_Clipping 模塊。 上面需要的更改在: https ://pypi.org/project/Selenium-Screenshot/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.