0

我在网页上有一张桌子。我想垂直和水平滚动它。但它不适合我。我尝试使用动作,但这也没有帮助。以下是我使用的操作代码

WebElement slider = driver.findElement(By.id(ID));
Actions builder = new Actions (driver);
builder.dragAndDropBy(slider, 30, 0).build().perform();

我也试过:

int height = slider.getSize().getHeight();
int width = slider.getSize().getWidth();
dragAndDrop = builder.clickAndHold(slider).moveByOffset(height,0).release().build();

奇怪的是,我分配滑块的任何值仅移动 6 行。

故事的HTML:

    <div id="pt1:pc1:md1::scroller" tabindex="-1" style="position: absolute; overflow: auto; z-index: 0; width: 770px; top: 46px; height: 187px; right: 0px;">
    <div style="width: 1260px; height: 6562px; visibility: hidden;"></div>
    </div>
    </div>
    </div>
4

1 回答 1

0

对于这些“瓶颈”(正如 Arek 在问题评论中描述的那样),请尝试 js 方法。对于我的 selenium 自动化项目,我编写了一组方法(使用 jsExecutor)。认为下面的想法应该对您有所帮助(提供的方法代码):

public void jsClick(Strning cssSelector) {
JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("var x = $(\'"+cssSelector+"\');");
        stringBuilder.append("x.click();");
        js.executeScript(stringBuilder.toString());
}

public String jsGetText(String cssSelector){
 JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("var x = $(\""+cssSelector+"\");");
        stringBuilder.append("return x.text().toString();")       ;


       String res= (String) js.executeScript(stringBuilder.toString());

return res;
}

public String jsGetColor(String css){

        JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("var x=$(\'"+css+"\');");
        stringBuilder.append("return x.css('color')");
        String res= (String) js.executeScript(stringBuilder.toString());
        return res;

    }

希望这对您有所帮助。

于 2012-09-25T10:59:56.820 回答