推荐学习书目
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 · Dec 6, 2023 · 1821 views
    This topic created in 928 days ago, the information mentioned may be changed or developed.
    ```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 replies    2023-12-06 18:25:19 +08:00
    jackaluo
        1
    jackaluo  
       Dec 6, 2023
    根据您的描述,您想要在 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)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3738 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 04:38 · PVG 12:38 · LAX 21:38 · JFK 00:38
    ♥ Do have faith in what you're doing.