首页 > 解决方案 > 在 Java 中使用 Selenium 3 定位城市的纬度和经度

问题描述

我正在自动化 Webmap 操作,为此我必须将纬度-经度传递给 Chrome 浏览器,以便单击特定区域。

我了解 Selenium 4 具有使用 Chrome DevTools 执行此操作的功能。但由于项目限制,我只能使用 Selenium 3.12 或 3.14 实现这种自动化。

这是我尝试过的以下代码:

 @Test
    public void testLocationClick() {
        int zoomActionCounter = 1;
        while (findElement(By.className("leaflet-control-zoom-in")).isEnabled()) {
            if (zoomActionCounter == 12) {
                break;
            }
            findElement(By.className("leaflet-control-zoom-in")).click();
            zoomActionCounter++;
        }
        ((LocationContext)getDriver()).setLocation(new Location(37.774929, -122.419416, 0));
        getDriver().get(SITE_URL);
     WebElement map = getDriver().findElement(By.className("leaflet-interactive"));
        waitForFewSeconds(1000);
        map.click();
        map.getLocation().getX();
        map.getLocation().getY();
        JavascriptExecutor js = (JavascriptExecutor) getDriver();
        js.executeScript("window.navigator.geolocation.getCurrentPosition=function(success){"+
                "var position = {\"coords\" : {\"latitude\": \"555\",\"longitude\": \"999\"}};"+
                "success(position);}");
}

在这段代码中,我无法实现结果。任何线索都会有所帮助。

我必须将纬度和经度坐标传递给测试方法,以便点击事件发生在该坐标位置。

标签: javaseleniumleafletgeolocationgeospatial

解决方案


推荐阅读