selenium元素截图(亲测有效)

selenium元素截图(亲测有效),第1张

selenium元素截图(在不滚动滑动条的情况下)可见即可截
from selenium import webdriver
from PIL import Image
driver_path=r"D:\pythonguanli\chromedriver_win32\chromedriver.exe"
driver=webdriver.Chrome(executable_path=driver_path)
driver.get("https://www.hao123.com/?src=from_pc_logon")
driver.maximize_window()
driver.execute_script('document.body.style.zoom="0.8"')  
#上面这个一定要有,没有的话元素截图可能会有误差
hao=driver.find_element_by_xpath('//*[@id="indexLogo"]/a/img')
hao.screenshot('5.png')
selenium元素截图(打开网页需要下滑才能看见的位置)
from selenium import webdriver
from PIL import Image
import time
driver_path=r"D:\pythonguanli\chromedriver_win32\chromedriver.exe"
url='https://www.douban.com/'
option=webdriver.ChromeOptions()
option.add_argument('headless')
#一定要加无头,不加报错
driver=webdriver.Chrome(options=option,executable_path=driver_path)
driver.get(url)
time.sleep(2)
dou=driver.find_element_by_xpath('//*[@id="anony-time"]/div/div[1]/h2/a')
dao.screenshot('5.png')

不用无头的话,需要下滑才能看到要去截图的元素方法待解决,在网上看了下资料好像是像素的原因,用无头的基本可以解决大多数元素截图

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/943394.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-18
下一篇 2022-05-18

发表评论

登录后才能评论

评论列表(0条)

保存