硒:拖放到SVG元素中

时间:2018-12-11 14:47:48

标签: javascript selenium selenium-webdriver cucumber

我试图将一个Webelement拖放到SVG“容器”内部,其中包含4个元素和4个多边形。多边形1 = g [1]多边形2 = g [2]等... 这是此容器的html代码:

<div class="leaflet-overlay-pane"><img class="leaflet-image-layer leaflet- 
zoom-animated" src="/ik-conf/layout/Planos1544448485901.png" style="opacity: 
1; transform: translate3d(509px, 280px, 0px); width: 524px; height: 348px;">
<svg class="leaflet-zoom-animated" width="1472" height="855" viewBox="35 26 
1472 855" style="transform: translate3d(35px, 26px, 0px);">
<g><path stroke-linejoin="round" stroke-linecap="round" fill-rule="evenodd" 
stroke="#FF9900" stroke-opacity="0.5" stroke-width="5" fill="#FF9900" fill- 
opacity="0.2" class="leaflet-clickable" d="M714 300L709 586L529 591L533 
300z"></path></g>
<g><path stroke-linejoin="round" stroke-linecap="round" fill-rule="evenodd" 
stroke="#FF9900" stroke-opacity="0.5" stroke-width="5" fill="#FF9900" fill- 
opacity="0.2" class="leaflet-clickable" d="M1011 305L1009 585L802 589L797 
302z"></path></g>
<g><path stroke-linejoin="round" stroke-linecap="round" fill-rule="evenodd" 
stroke="#00FFD5" stroke-opacity="0.5" stroke-width="5" fill="#00FFD5" fill- 
opacity="0.2" class="leaflet-clickable" d="M690 444L688 554L544 552L544 
433z"></path></g>
<g><path stroke-linejoin="round" stroke-linecap="round" fill-rule="evenodd" 
stroke="#FFFFFF" stroke-opacity="0.5" stroke-width="5" fill="#FFFFFF" fill- 
opacity="0.2" class="leaflet-clickable" d="M989 321L987 444L816 435L816 
320z"></path></g>
</svg><div class="leaflet-draw-guides"></div></div>

每个多边形(G)都由XPATH和/或CSS定位,并且我总是收到NoSuchElementException

这是我正在运行的代码:

public void dragAndDropPlanos(String nombre, WebElement poligono) {
    wait.until(ExpectedConditions.numberOfElementsToBe(By.xpath("//*[contains(text(),'" + nombre + "')]"), 1));
    WebElement element = driver.findElement(By.xpath("//*[contains(text(),'" + nombre + "')]"));
    js.executeScript("arguments[0].scrollIntoView(true);", element);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    WebElement element1 = driver.findElement(By.xpath("//*[contains(text(),'" + nombre + "')]"));
    wait.until(ExpectedConditions.visibilityOf(element1));
//        WebElement target = poligono; 
    wait.until(ExpectedConditions.visibilityOf(poligono));
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
    }
    WebElement element3 = driver.findElement(By.xpath("//*[contains(text(),'" + nombre + "')]"));
    (new Actions(driver)).dragAndDrop(element3, poligono).perform();
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
    }
}

因此,您可以看到我正在传递一个字符串名称,该字符串名称用于连接到xpath来定位我要拖动的元素,并且还作为要传递给我要放置的多边形的webelement传递。如我所说,这些多边形已正确放置并作为Weblement存储在公共类中。我发现svg可能放在不同的iframe中,我尝试进行切换,但事实并非如此,它只是在div中,我应该能够通过xpath到达它

有人可以在这里带来一些启发吗? 预先感谢

1 个答案:

答案 0 :(得分:0)

可能是由于条件太宽和文本可疑而发现了不正确的WebElement。

  //*[contains(text(),'" + nombre + "')]

首先,如果您要查找可打印的文本,则此定位器将找不到任何内容,因为SVG中没有文本。其次,为什么要查找带有文本的 any 元素?尝试先按标签名称搜索,然后检查是否返回标签。

   List<WebElement> elements = driver.findElements(By.tagName("g"));

此外,即使找到了 g 元素,某些驱动程序(例如PhantomJS)也可能无法对其进行操作,因此您必须获取内部元素(路径) 为此。