【问题标题】:draw a line between two boxes which are draggable in selenium webdriver using Java code使用 Java 代码在 selenium webdriver 中可拖动的两个框之间画一条线
【发布时间】:2015-05-07 13:30:50
【问题描述】:

我正在开发 selenium 网络驱动程序,但我的应用程序不支持 css 选择器。在我的应用程序中,我有流程图页面,我需要在其中添加流程图。我在流程图页面中拖放了两个矩形框,每个矩形框都有四个箭头。 使用源矩形框的箭头之一应连接到目标矩形框的顶部箭头。我不知道如何连接两个盒子。此外,如果我们将其中一个箭头从源点拖动到目标点,它会在两者之间画一条线。

我尝试了很多方法,但无法在两个盒子之间连接和画线。我应该只用 Java 编写代码。请帮忙。

另外,我观察到,当我试图找到两个不同矩形框的箭头坐标时,它会显示相同的坐标 i,e

两个箭头(源和目标)的位置显示相同的坐标(-9317,-9973) (-9317, -9973)

为什么它显示负 x 和 y 点。请回复我只用 JAVA 编写代码,因为我在我的程序中使用 JAVA 我尝试使用下面的代码在 JAVA 中的两个流程图框之间画一条线 - 使用 selenium Webdriver

WebElement selectarrow = driver.findElement(By.xpath("//circle[@cx='10']"));
        System.out.println(selectarrow.getLocation()); 

 Action drawAction = builder.moveToElement(selectarrow, 250, 50)  
                    .clickAndHold()
                    .moveByOffset(250,50)
                    .moveByOffset(270,110)
                    .release()
                    .build();
          drawAction.perform();

使用选择箭头作为源点和 250,50 作为该区域的目标点来绘制线。

【问题讨论】:

  • 你好。欢迎来到 SO。在这里,我们不会为您编写代码。请阅读stackoverflow.com/help/how-to-ask
  • 我尝试使用下面的代码在 JAVA 中的两个流程图框之间画一条线 - 使用 selenium webdriverWebElement selectarrow = driver.findElement(By.xpath("//circle[@cx='10'] ")); System.out.println(selectarrow.getLocation());
  • 如果没有看到 HTML,那段代码就没有多大意义。另外,我看不出代码有什么问题。为什么你认为这是错误的?
  • 我也不明白你想如何用 Selenium 画线。 Selenium 不绘制。您可以使用 Selenium 只能向网页发送事件来模拟用户。
  • 我们应该使用 Java 代码进行绘制。如果我知道,我会把代码贴在这里。

标签: java jquery selenium


【解决方案1】:

首先,让我们弄清楚一些术语:Java 和 Selenium 不画任何东西。 Java 用于执行 Selenium 代码。 Selenium 只是向浏览器发送事件。

Selenium 的主要问题是您正在做锁孔手术:当出现问题时几乎没有反馈,很难看到您在做什么,即使一切看起来都很好,它仍然可能会损坏。请记住这一点。

您的代码在做什么:您正在创建一系列鼠标单击和拖动事件,具体而言:

  • 相对于selectarrow的上/左角转到250、50
  • 点击并按住
  • 向右走 250,向下走 50
  • 向右行驶 270 度,向下行驶 110 度
  • 松开鼠标左键

这不是你所描述的你想要做的。执行您描述的代码如下所示:

Action drawAction = builder.moveToElement(selectarrow, 0, 0)  
                .clickAndHold()
                .moveByOffset(250,50)
                .release()
                .build();
drawAction.perform();

请注意,您可能希望使用5,5 而不是0,0 来确保您在元素内。但是当你这样做时,你可能还必须修改moveByOffset(),否则最后一点可能会被取消。

【讨论】:

  • 感谢您的详细解释。我尝试使用在两个矩形框之间画一条线的代码。
【解决方案2】:

要在两个框之间画线,

WebElement segmentPB = driver.findElement(By.cssSelector("#dojoUnique3 > img"));
                (new Actions(driver)).dragAndDropBy(segmentPB,300, 175).perform();

                 WebElement droppedSegmentBox = driver.findElement(By.cssSelector("#Flowchart > div:nth-child(3) > svg > g:nth-child(2) > g:nth-child(2) > g:nth-child(2) > g > g:nth-child(2) > g:nth-child(1) > image:nth-child(1)"));
                 System.out.println(droppedSegmentBox.getLocation());   

                Actions builder = new Actions(driver);
                builder.moveToElement(droppedSelectBox).perform();

                droppedSelectBox.click();

                WebElement selectArrow = driver.findElement(By.cssSelector("#Flowchart > div:nth-child(3) > svg > g:nth-child(2) > g:nth-child(3) > g:nth-child(2) > g:nth-child(4) > g > g:nth-child(3) > g > g > path:nth-child(2)"));

                System.out.println(selectArrow.getLocation());  
                // To draw the line between Two Process Boxes
                (new Actions(driver)).dragAndDrop(selectArrow, droppedSegmentBox).build().perform();

【讨论】:

  • 是的,这是解决我问题的代码,我一直在尝试使用此代码,但出现了一些小错误...即 movetoElement(droppedSelectBox).perform 被错过了,我的团队发现了已纠正。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多