我在网页上有一张桌子。我想垂直和水平滚动它。但它不适合我。 我尝试过使用动作,但这也没有帮助。以下是我使用的动作代码
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>
答案 0 :(得分: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;
}
希望这能以某种方式帮助你。