9

using selenium execute JavaScript controll browser scroll

 3 years ago
source link: http://jeffsui.github.io/2020/04/10/using-selenium-execute-JavaScript-controll-browser-scroll/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

selenium中使用JavaScript控制浏览器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//移动到元素element对象的“顶端”与当前窗口的“顶部”对齐
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

//移动到元素element对象的“底端”与当前窗口的“底部”对齐
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(false);", element);

//移动到页面最底部
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");

//移动到指定的坐标(相对当前的坐标移动)
((JavascriptExecutor) driver).executeScript("window.scrollBy(0, 700)");
Thread.sleep(3000);
//结合上面的scrollBy语句,相当于移动到700+800=1600像素位置
((JavascriptExecutor) driver).executeScript("window.scrollBy(0, 800)");

//移动到窗口绝对位置坐标,如下移动到纵坐标1600像素位置
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, 1600)");
Thread.sleep(3000);
//结合上面的scrollTo语句,仍然移动到纵坐标1200像素位置
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, 1200)");

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK