如何在Canvas(HTML)中使用Java检查点击位置。

huangapple go评论137阅读模式
英文:

How to check where is clicked in canvas(HTML) using Java

问题

我有一个窗口里面有一个画布。窗口是全屏的,但画布不是全尺寸的。我想在画布上点击特定位置,所以我使用以下代码:

Actions action = new Actions(driver).moveToElement(canvas, 0, 0).moveByOffset(849,341).click();

问题是,使用上面的代码不会点击特定位置,我不知道点击的位置在哪里。我已经尝试使用 Page Ruler 来定位指针,但在这种情况下它不起作用。

我希望有一种解决方案可以突出显示或显示指针的位置。

英文:

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

如何在Canvas(HTML)中使用Java检查点击位置。

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

得分: 0

根据提供的HTML:

如何在Canvas(HTML)中使用Java检查点击位置。

所需的 <canvas> 元素位于一个 <iframe> 内,因此您需要:

  • 触发 WebDriverWait 以等待所需的 frameToBeAvailableAndSwitchToIt

  • 触发 WebDriverWait 以等待 visibilityOfElementLocated,如下所示:

  • 您可以使用以下任一 定位策略

    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); // -> 返回 849
    System.out.println(canvas.getSize().height/2); // -> 返回 341

英文:

As per the given HTML:

如何在Canvas(HTML)中使用Java检查点击位置。

The desired &lt;canvas&gt; element is within is within a &lt;iframe&gt; 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(&quot;div.dialogMaster&quot;)));
    WebElement canvas = new WebDriverWait(Util.setup.driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.tagName(&quot;canvas&quot;))).get(0);
    System.out.println(canvas.getSize().width/2); // -&gt; returns 849
    System.out.println(canvas.getSize().height/2); // -&gt; returns 341
    

huangapple
  • 本文由 发表于 2023年3月7日 05:14:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定