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
guoguobaba
V2EX  ›  Python

问个 Python selenium 的问题

  •  
  •   guoguobaba · 146 天前 · 922 次点击
    这是一个创建于 146 天前的主题,其中的信息可能已经有所发展或是发生改变。
    ```python
    for x, y in param:
    center_x = int(x) - div_size['width'] / 2
    center_y = int(y) - div_size['height'] / 2
    logging.info("click at (%.2f, %.2f)" % (center_x, center_y))
    action_chains.move_to_element_with_offset(element, center_x,center_y).click().perform()
    time.sleep(random.randint(0, 10) / 10)
    ```
    按文档说 move_to_element_with_offset 是 Move the mouse by an offset of the
    specified element. Offsets are relative to the top-left corner of the element.
    是以左上为原点,但是我这里必须以 element 中心为原点才能成功呢?
    1 条回复    2023-12-06 18:25:19 +08:00
    jackaluo
        1
    jackaluo  
       145 天前
    根据您的描述,您想要在 Selenium 中使用 move_to_element_with_offset 方法,但是您希望以元素的中心点作为原点,而不是左上角。默认情况下,move_to_element_with_offset 方法确实是以元素的左上角作为原点的。

    如果您想以元素的中心为原点移动鼠标,您可以自己计算中心点的坐标,然后将偏移量设置为中心点与目标点之间的差值。这是一个示例代码:
    for x, y in param:
    center_x = int(x)
    center_y = int(y)
    logging.info("click at (%.2f, %.2f)" % (center_x, center_y))

    # 计算偏移量,使其相对于元素中心
    offset_x = center_x - element.size['width'] / 2
    offset_y = center_y - element.size['height'] / 2

    action_chains.move_to_element_with_offset(element, offset_x, offset_y).click().perform()
    time.sleep(random.randint(0, 10) / 10)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5229 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 08:55 · PVG 16:55 · LAX 01:55 · JFK 04:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.