Selenium Webdriver 将鼠标移动到 Point [英] Selenium Webdriver move mouse to Point

查看:60
本文介绍了Selenium Webdriver 将鼠标移动到 Point的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将光标移动到一个点 (org.openqa.selenium.Point),该点已通过检查实时图表上标记的出现来设置,我可以从中无法获取详细信息,但可以找到的 X 和 Y 坐标.

I am currently trying to move the cursor to a point (org.openqa.selenium.Point) that has been set by checking for an occurrence of a marker on a live chart from which I can get no details but can find the X and Y coordinates of.

如何移动鼠标以将鼠标悬停在所述点上以打开底层 JavaScript 菜单?

How can I move to the mouse to hover over said point to open the underlying JavaScript menu?

//finds marker on the current web page

Point image = page.findImage("C:\Pictures\marker.png") ;

//move mouse to this x,y location 

driver.getMouse().mouseMove((Coordinates) image);

这不起作用,因为 Point 不能转换为 org.openqa.selenium.interactions.internal.Coordinates.

This doesnt work as Point cannot be cast to org.openqa.selenium.interactions.internal.Coordinates.

推荐答案

恕我直言,您应该注意 Robot.class

IMHO you should pay your attention to Robot.class

如果你想物理移动鼠标指针,你需要使用Robot类采取不同的方法

  Point coordinates = driver.findElement(By.id("ctl00_portalmaster_txtUserName")).getLocation();
  Robot robot = new Robot();
  robot.mouseMove(coordinates.getX(),coordinates.getY()+120);

Webdriver 提供文档坐标,其中 Robot 类基于屏幕坐标,所以我添加了 +120 来补偿浏览器标题.
屏幕坐标:这些是从用户计算机屏幕左上角开始测量的坐标.您很少会得到坐标 (0,0),因为它通常在浏览器窗口之外.大约唯一需要这些坐标的时间是,如果您想将新创建的浏览器窗口放置在用户单击的位置.在所有浏览器中,这些都在 event.screenXevent.screenY 中.
窗口坐标:这些是从浏览器内容区域的左上角开始测量的坐标.如果窗口滚动,垂直或水平,这将与文档的左上角不同.这很少是您想要的.在所有浏览器中,这些都在 event.clientX 和 event.clientY.
文档坐标:这些是从 HTML 文档的左上角开始测量的坐标.这些是您最常用的坐标,因为这是定义文档的坐标系.

Webdriver provide document coordinates, where as Robot class is based on Screen coordinates, so I have added +120 to compensate the browser header.
Screen Coordinates: These are coordinates measured from the top left corner of the user's computer screen. You'd rarely get coordinates (0,0) because that is usually outside the browser window. About the only time you'd want these coordinates is if you want to position a newly created browser window at the point where the user clicked. In all browsers these are in event.screenX and event.screenY.
Window Coordinates: These are coordinates measured from the top left corner of the browser's content area. If the window is scrolled, vertically or horizontally, this will be different from the top left corner of the document. This is rarely what you want. In all browsers these are in event.clientX and event.clientY.
Document Coordinates: These are coordinates measured from the top left corner of the HTML Document. These are the coordinates that you most frequently want, since that is the coordinate system in which the document is defined.

更多详情,您可以在此处

希望对你有帮助.

这篇关于Selenium Webdriver 将鼠标移动到 Point的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆