selenium之js

1.       修改任意html的属性

2.       操作滚动栏/滚动到执行高度或element

"document.documentElement.scrollTop=" + height;

WebElement target = driver.findElement(by);

js.executeScript("arguments[0].scrollIntoView();",target);

3.       检查页面是否load完毕

(JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete")

4.       Js获取页面元素

Document.getElementById   document.getElementsByname("name1")[0]  s

ByTagName   ByClassName

 

滚动

((JavascriptExecutor) driver).executeScript("scroll(0,250);");  //-250

((JavascriptExecutor) driver).executeScript("scroll(250,0);");

((JavascriptExecutor) driver).executeScript("window.scrollBy(0,250)", "");

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();" ,webElement);

 

WebElement element = driver.findElement(by);

int elementPosition = element.getLocation().getY();

String js = String.format("window.scroll(0, %s)", elementPosition);

 

执行:

public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"resource/chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://localhost:8081/tms/welcome/manager");
Thread.sleep(4000);

Object aa=jsReturner("addFun()");
Thread.sleep(10000);
jsReturner("$('#form').form.onSubmit");
System.out.println("ssssssssssssssssss");
Thread.sleep(4000);
}

public static Object jsReturner(String js) {
return ((JavascriptExecutor) driver).executeScript(js);
}

posted @ 2016-03-28 22:04  q55091  阅读(208)  评论(0编辑  收藏  举报