V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
littlepython
V2EX  ›  Python

python3 使用 selenium 截屏后再截取指定坐标图片定位不准确是为什么

  •  
  •   littlepython · 2020-04-16 19:55:48 +08:00 · 3091 次点击
    这是一个创建于 1442 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想截取一下验证码的图片,但是截图后和验证码的位置不对应,不知道是不是写法有问题,还是有其余的因素在影响,selenium 用的 Chrome 浏览器 附上操作代码 browser.save_screenshot('bdbutton.png') element = browser.find_element_by_xpath('//*[@id="login-yzm_img"]') # 找到验证码图片 print(element.location) # 打印元素坐标 print(element.size) # 打印元素大小 left = element.location['x'] top = element.location['y'] right = element.location['x'] + element.size['width'] bottom = element.location['y'] + element.size['height']

    im = Image.open('bdbutton.png') im = im.crop((left, top, right, bottom)) im.show() im.save('bdbutton.png')

    12 条回复    2020-04-21 20:47:22 +08:00
    ClericPy
        1
    ClericPy  
       2020-04-16 20:00:06 +08:00   ❤️ 1
    留个网址? 我试试我直接原生 cdp 截图准不准

    如果盲猜的话, 怀疑和截图参数 scale 有关, 但是不确定 selenium 走的是 driver 内置的截图还是也同样走了 cdp
    littlepython
        2
    littlepython  
    OP
       2020-04-16 20:06:55 +08:00
    @ClericPy 就芒果 tv 短信登录的那儿的验证码截图
    ClericPy
        3
    ClericPy  
       2020-04-16 21:37:05 +08:00
    unixeno
        4
    unixeno  
       2020-04-16 21:56:52 +08:00 via Android   ❤️ 1
    你系统开缩放了?
    dudu2017
        5
    dudu2017  
       2020-04-16 22:04:25 +08:00
    指定一个窗口大小试试。
    littlepython
        6
    littlepython  
    OP
       2020-04-17 07:51:06 +08:00
    @ClericPy 老哥 你这段代码我在 windows 运行直接报错,我是有什么没设置吗
    littlepython
        7
    littlepython  
    OP
       2020-04-17 09:21:16 +08:00
    @unixeno 系统的推荐是 200%, 把坐标改成 2 就好了,就是不知道怎么能通用
    littlepython
        8
    littlepython  
    OP
       2020-04-17 09:22:09 +08:00
    @dudu2017 和系统的缩放有关 不知道怎么设置一个通用的
    ClericPy
        9
    ClericPy  
       2020-04-17 10:51:04 +08:00   ❤️ 1
    @littlepython #6 代码就是随手发的, 因为我没用 selenium, 走的是默认的 Chrome cdp, 可以忽略

    我前面说的 scale 参数就是页面缩放的问题, 你要么把 scale 改为 1, 要么就通过 selenium 或者 js 拿到当前缩放 scale 值, 在截图时候传入正确的就可以了
    littlepython
        10
    littlepython  
    OP
       2020-04-17 11:05:48 +08:00
    @ClericPy 好的 我试试
    fzhyzamt
        11
    fzhyzamt  
       2020-04-17 11:18:41 +08:00
    Mac 下试了一下是正常的,可能确实是缩放的原因
    JohnTao
        12
    JohnTao  
       2020-04-21 20:47:22 +08:00 via iPhone
    用 xpath 定位
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1594 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:53 · PVG 00:53 · LAX 09:53 · JFK 12:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.