如何使用Selenium通过点击和拖动来导航地图

huangapple 未分类评论61阅读模式
标题翻译

How to navigate a map with Selenium via clicking and dragging

问题

我正在使用Selenium和Java尝试自动化一些谷歌地图导航。我想点击并保持地图,然后朝某个方向移动,然后释放光标。这应该会将地图在页面上移动。

到目前为止,我有以下代码:

WebElement canvasElement = driver.findElement(map);
Actions builder = new Actions(driver.getWebDriver());
builder.moveToElement(canvasElement).clickAndHold().moveByOffset(300, 0).release().perform();

然而,页面上没有任何东西移动。我知道我选择地图的方式是正确的,因为如果我只执行以下代码:

builder.moveToElement(canvasElement).clickAndHold().moveByOffset(300, 0).perform();

那么当我将鼠标悬停在地图上时,地图会移动,而无需点击左键,因为它仍处于点击并保持状态,未释放。不确定为什么moveByOffset在这种特定情况下不起作用。任何建议都将是很好的。

英文翻译

I am using Selenium and Java to try and automate some google maps navigation. I want to click and hold on the map, then move to a certain direction, then release the cursor. This should move the map across the page.

So far I have

WebElement canvasElement = driver.findElement(map);
Actions builder = new Actions(driver.getWebDriver());
builder.moveToElement(canvasElement).clickAndHold().moveByOffset(300, 0).release().perform();

However, nothing on the page moves. I know I am selecting the map correctly since if I just do

    builder.moveToElement(canvasElement).clickAndHold().moveByOffset(300, 0).perform();

then when I do mouse over the map, the map moves without me having to click the left mouse button because it is still in the click and hold state and never released. Not sure why the moveByOffset is not working for this particular case. Any suggestions would be great

答案1

得分: 0

在地图上点击并按住,然后向某个方向移动,然后释放光标,一旦你创建了这一系列的操作,你还需要调用build()方法,你可以使用以下解决方案:

WebElement canvasElement = driver.findElement(map);
new Actions(driver).moveToElement(canvasElement, 0, 0).clickAndHold().moveByOffset(300, 0).release().build().perform();
英文翻译

To click and hold on the map, then move to a certain direction, then release the cursor, one you create the chain of actions, you need to invoke the build() method as well and you can use the following solution:'

WebElement canvasElement = driver.findElement(map);
new Actions(driver).moveToElement(canvasElement, 0, 0).clickAndHold().moveByOffset(300, 0).release().build().perform();

huangapple
  • 本文由 发表于 2020年3月4日 04:35:03
  • 转载请务必保留本文链接:https://java.coder-hub.com/60515094.html
匿名

发表评论

匿名网友

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

确定