1

I have a canvas inside a window. The window is full screen, but the canvas is not the full size. I want to click a specific position in canvas so I use bellow:
Actions action = new Actions(driver).moveToElement(canvas, 0, 0).moveByOffset(849,341).click();

enter image description here

WebElement iframe = driver.findElements(By.tagName("iframe")).get(0);
driver.switchTo().frame(iframe);
WebElement canvas = driver.findElements(By.tagName("canvas")).get(0);
System.out.println(canvas.getSize().width/2); // -> returns 849
System.out.println(canvas.getSize().height/2); // -> returns 341

The problem is, the specific position isn't clicked and I don't know where is clicked just by using the code above. I've used Page Ruler to locate pointer but it didn't work in this situaion.

I'm hoping there is a solution which will highlight or show the location of pointer.

1 Answers1

0

As per the given HTML:

html

The desired <canvas> element is within is within a <iframe> so you have to:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.

  • Induce WebDriverWait for the visibilityOfElementLocated as follows:

  • You can use either of the following Locator Strategies:

    new WebDriverWait(Util.setup.driver, Duration.ofSeconds(10)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("div.dialogMaster")));
    WebElement canvas = new WebDriverWait(Util.setup.driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.tagName("canvas"))).get(0);
    System.out.println(canvas.getSize().width/2); // -> returns 849
    System.out.println(canvas.getSize().height/2); // -> returns 341
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I forgot to add finding iframe code. Please check the updated code – codename_zoe Mar 06 '23 at 21:40
  • Thank you, this one works. However, I want to know where is clicked. I used this code to see if the specifil location on canvas is clicked but no idea where is clicked: Actions action = new Actions(driver).moveToElement(canvas, 0, 0).moveByOffset(849,341).click(); – codename_zoe Mar 06 '23 at 21:59
  • As you are using `moveByOffset(849,341)` ofcoarse the click happens at that offset with respect to the upper-left corner of the canvas having the coordinates `(0,0)`. See [details](https://stackoverflow.com/a/59923053/7429447). – undetected Selenium Mar 06 '23 at 22:03