seleniumselenium-webdriverui-automationselenide

Selenide - Click in the upper left corner of the button


I have a question about Selenide. Is there possible to make a click not to the center of the button, but on some corner?

Of course, I can do it using offset $(element).click(ClickOptions.usingDefaultMethod().offset(x, y))

But it is not appropriate for me because I want to do a universal method without connection with some element.

Thanks.


Solution

  • When you find an element you have it's rect. Just calculate half of this rect width end height and move from the center of the element:

    WebElement we = browser.findElement(By.xpath(xPath));
    int H = we.getRect().height;
    int W = we.getRect().width;
    H = H / 2;
    W = W / 2;         
    Actions builder = new Actions(browser);
    builder.moveToElement(we, -W,-H ).click().build().perform();