英文:
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();
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>
元素位于一个 <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:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论