标题翻译
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();
专注分享java语言的经验与见解,让所有开发者获益!
评论