硒Javadoc for Actions.moveToElement表示xOffset和yOffset参数的含义如下。
xOffset - Offset from the top-left corner. A negative value means coordinates left from the element.
yOffset - Offset from the top-left corner. A negative value means coordinates above the element.考虑下面的程序,在Linux上运行Firefox。
public class FirefoxTest {
public static void main(String[] args) {
// Set up driver
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.get("http://www.google.com");
WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));
// Perform a move and click action to see where it lands.
Actions moveAndClick = new Actions(driver).moveToElement(element,0,0).doubleClick();
moveAndClick.perform();
}
}当运行以下程序时,双击发生在搜索框的中间,而不是左上角(我知道这一点,因为我注入了JS来记录单击的位置)。此外,在运行程序的终端中输出以下消息。
org.openqa.selenium.interactions.Actions moveToElement
INFO: When using the W3C Action commands, offsets are from the center of element是否可以以编程方式确定偏移量是来自Actions.moveToElement**?**元素的中间还是左上角?
发布于 2018-10-16 13:14:02
首先,当调用W3C时,第一组日志确认方言为,如下所示:
org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C您是正确的,因为JavaDocsformoveToElement()仍然提到:
public Actions moveToElement(WebElement target, int xOffset, int yOffset)
Description:
Moves the mouse to an offset from the top-left corner of the element. The element is scrolled into view and its location is calculated using getBoundingClientRect.
Parameters:
target - element to move to.
xOffset - Offset from the top-left corner. A negative value means coordinates left from the element.
yOffset - Offset from the top-left corner. A negative value means coordinates above the element.
Returns:
A self reference.我可以将你的问题转载如下:
正如@Andreas在讨论中指出的,偏移是从元素的中心,而不是从左上角。 @FlorentB。明确指出:
1. Let element be equal to the result of trying to get a known connected element with argument origin.
2. Let x element and y element be the result of calculating the in-view center point of element.
3. Let x equal x element + x offset, and y equal y element + y offset.
视点中心点
元素的视图中心点是矩形的起始位置,即元素的第一个DOM客户端矩形与初始视图端口之间的交集。给定已知的视图元素,计算为:
因此可以得出结论,偏移量来自center,但Jave尚未更新。
发布于 2020-07-10 07:46:12
如果问题是单击元素的左上角,那么下面的代码片段就可以了。
WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));
Actions moveAndClick = new
int yOffset=element.getRect().height/-2;//top
int xOffset=element.getRect().width/-2;//left
Actions(driver).moveToElement(element,xOffset,yOffset).doubleClick().perform();https://stackoverflow.com/questions/51974620
复制相似问题