Selenium - 如何在具有双坐标/浮点坐标的点上悬停?

时间:2017-06-13 01:36:17

标签: java selenium selenium-webdriver

目前,Selenium的Actions类仅将此作为moveToElement的函数:

public Actions moveToElement(WebElement toElement, int xOffset, int yOffset {
    action.addAction(new MoveToOffsetAction(mouse, (Locatable) toElement, xOffset, yOffset));
    return this;
}

但是,网站的坐标有float坐标(例如(200.5,0))。有什么方法可以徘徊到这一点吗?

[编辑]: 这是我试图做的方法。使用整数坐标完美地工作

public void hoverPt(String xAxis) throws Exception //Use Build Interaction
{
int x = Integer.parseInt(xAxis);
WebDriver driver = WebManager.getDriver();
WebElement lineGraph = driver.findElement(By.xpath("[xpath of chart plane]"));
Actions builder = new Actions(driver);
builder.moveToElement(lineGraph, x, 0).build().perform();
Thread.sleep(5000);
}

字符串xAxis 是从Excel工作表中获取的,然后我只是将其解析为int,因此我可以将其参数化为 moveToElement

1 个答案:

答案 0 :(得分:0)

您可以使用Robot类:

    Point p = toElement.getLocation();
    int x = p.getX();
    int y = p.getY();
    Dimension d = toElement.getSize();
    int h = d.getHeight();
    int w = d.getWidth();
    Robot r = new Robot();
    r.mouseMove(x + (w/2), y+(h/2) +80);