1

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.

robert0801
  • 140
  • 1
  • 12

1 Answers1

2

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();
Tal Angel
  • 1,301
  • 3
  • 29
  • 63
  • height/2 is wrong because of oveflow: scroll in parent and element height bigger than screen height (when a part of element hidden) – mixalbl4 Jan 31 '22 at 08:02
  • @mixalbl4 My code works. Your example is a rare case. – Tal Angel Jan 31 '22 at 09:03
  • try to use it in element with height 2000px and you will see incorrect results, because screen height is 900px and element height is 2000px, viewport center is 450, not 1000px like in your case)) – mixalbl4 Jan 31 '22 at 09:09
  • @mixalbl4 So please write your own answer with your full code. – Tal Angel Jan 31 '22 at 09:11
  • Done :) My own answer here: https://stackoverflow.com/a/70937795/2051938 – mixalbl4 Feb 01 '22 at 08:59