diego
diego

Reputation: 150

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.

Upvotes: 1

Views: 2579

Answers (2)

max
max

Reputation: 1266

I had a similar issue where an element was only half displayed in a scroll-able / lazy loaded drop-down list. I know the following code isn't nice but it works for my test scenario:

    private void advancedClick(SelenideElement element) {
        int height = (element.getRect().height / 2) -1;
        int width = (element.getRect().width / 2) - 1;
        try {
            // click left / top
            element.click(ClickOptions.withOffset(-height, -width)); 
        } catch(org.openqa.selenium.ElementClickInterceptedException et) {
            try {
                // click middle
                element.click(); 
            } catch(org.openqa.selenium.ElementClickInterceptedException em) {
                // click right / bottom
                element.click(ClickOptions.withOffset(height, width));
            }
        }
    }

enter image description here

Upvotes: 0

Tal Angel
Tal Angel

Reputation: 1782

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();

Upvotes: 2

Related Questions