2013-02-12 72 views
1

我想模擬一個簡單的鼠標點擊並在svg元素內拖動。 我設法得到我的起點和終點的座標,絕對(窗口座標)和相對於封裝svg元素。 這裏是我使用來模擬鼠標代碼:SVG Selenium點擊不起作用

Actions builder = new Actions(driver); 
    builder.moveToElement(area, xStart, yStart); 
    builder.clickAndHold(); 
    builder.moveToElement(area, xStop, yStop); 
    builder.release(); 

    Action setFilter = builder.build(); 
    setFilter.perform(); 

凡區域是代表我的SVG一個WebElement和座標是相對於該元素。請注意:

area.getLocation(); // returns null 

這讓我想知道webdriver是否能夠找到該元素。所以,我想絕對座標:

Actions builder = new Actions(driver); 
    builder.moveByOffset(chart.getLocation().x + xStart, chart.getLocation().y + yStart); 
    builder.clickAndHold(); 
    builder.moveByOffset(xStop - xStart, yStop - yStart); 
    builder.release(); 

    Action setFilter = builder.build(); 
    setFilter.perform(); 

其中圖表是div周圍的SVG元素(注意,在div位置和SVG的位置之間的偏移量僅爲10像素,並且不顯著)。這也沒有效果,我也試圖通過相對位置,但仍然沒有運氣。

我在這裏做錯了什麼?

+0

是否有可能使用「的getLocation()」包含SVG而不是元素? – Nashibukasan 2013-02-12 22:51:20

+0

嗨@Nashibukasan是的,這是可能的。我在包含div上使用getLocation來獲取svg元素的(近似)座標。 svg及其後代節點將返回null。 – Renaud 2013-02-13 09:26:14

+0

是否可以使用返回的近似值以數學方式查找其他元素的座標?至於實際問題是什麼,我不太確定,但有興趣看看結果。 – Nashibukasan 2013-02-13 21:08:28

回答

0

我得到了一個黑客的工作,但它需要在前臺打開網頁。如果你同時做其他任何事情,它可能會破壞測試,我不能說如果它遠程運行,它是否會工作。

這裏是什麼樣子:

Robot robert = new Robot(); 
    robert.mouseMove(xStart, yStart); 
    // full click once to get focus on the window 
    robert.mousePress(MouseEvent.BUTTON1_MASK); 
    robert.mouseRelease(MouseEvent.BUTTON1_MASK); 
    // then set the filter 
    robert.mousePress(MouseEvent.BUTTON1_MASK); 
    robert.mouseMove(xStop, yStop); 
    robert.mouseRelease(MouseEvent.BUTTON1_MASK);