[JAVA #11] [Web Automation #11] Controlling STEEMIT web pages with Selenium #9 Scroll [CN] by june0620

View this thread on steempeak.com
· @june0620 · (edited)
$5.87
[JAVA #11] [Web Automation #11] Controlling STEEMIT web pages with Selenium #9 Scroll [CN]
![redsjavahouse1591357_1280.jpg](https://files.steempeak.com/file/steempeak/june0620/DKldjbG3-reds-java-house-1591357_1280.jpg)
image source: [pixabay](https://pixabay.com/photos/red-s-java-house-eatery-cafe-1591357/0)
***

selenium没有控制滚动条的内置函数。
可以用 JavascriptExecutor 借口来实现滚动条控制,它可以用js随时随地控制selenium函数无法控制的部分。 
貌似可以实现 javascript 所有的功能。 

今天就先简单的学一下滚动条的控制。如懂  javascript,就更容易学会。

JavascriptExecutor接口是由继承 WebDriver接口的 RemoteWebDriver 类最终实现的,可以cast Webdriver来创建生成变量,之后可以用 executeScript()来控制。参数有两种,第一个可以把javascript代码转成String类型来给入,第二个是可变参数 Object,有没有无所谓。 👇

```
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("window.scrollTo(0, 1000)");
```

可以用如下方法控制滚动条。 
###### 1. 用 javascript的 window scrollTo, scrollBy控制
👇参数为 x坐标, y坐标,可控制横竖滚动。
```
je.executeScript("window.scrollTo(0, 1000)");
```
👇也可以用变量稍微改下。(用 javascript来获取滚动条高度并把值写入y坐标,即滚动到最低。)
```
je.executeScript("window.scrollTo(0, document.body.scrollHeight)");
```
👇 或者获取当前滚动条x,y值再-+ alpha。
```
je.executeScript("window.scrollTo(0, window.scrollY + 700)");
```

###### 2.滚动到特定元素。
👇 用 javascript 找元素并用 scrollIntoView() 函数滚动到此。 
```
je.executeScript("document.getElementById('content').scrollIntoView(true)");
```

👇 再或者先用 selenium查找元素,之后把元素放 executeScript的第二个参数。 
```
WebElement ele = driver.findElements(By.id("posts_list")).get(0);
je.executeScript("arguments[0].scrollIntoView()",ele );
```
###### 3. 键盘控制滚动
👇 控制键盘上的 home/end 键来滚动。 sendkeys() 函数可以调用键盘,元素用html元素即可。 
```
WebElement html = driver.findElement(By.tagName("html"));
html.sendKeys(Keys.HOME);
html.sendKeys(Keys.PAGE_DOWN);
```

经调试,上述例子都可以正常控制滚动,个人觉得第二个最好。 
```
package com.steem.webatuo;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;

public class Steemit {
	
	public static void main(String[] args) throws InterruptedException {
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.get("https://steemit.com");
		
		Wait<WebDriver> wait = new WebDriverWait(driver, 10);

		Thread.sleep(2000);
		JavascriptExecutor je = (JavascriptExecutor) driver;
		je.executeScript("window.scrollTo(0, document.body.scrollHeight)");
		
		Thread.sleep(2000);
		je.executeScript("window.scrollTo(0, window.scrollY + 700)");
		
		Thread.sleep(2000);
		je.executeScript("document.getElementById('content').scrollIntoView(true)");
		
		Thread.sleep(2000);
		je.executeScript("window.scrollBy(0, document.body.scrollHeight)");
		
		Thread.sleep(2000);
		WebElement html = driver.findElement(By.tagName("html"));
		html.sendKeys(Keys.HOME);

		Thread.sleep(2000);
		html.sendKeys(Keys.PAGE_DOWN);
		
		Thread.sleep(2000);
		WebElement ele = driver.findElements(By.id("posts_list")).get(0);
		je.executeScript("arguments[0].scrollIntoView()",ele );
		driver.quit();
	}

}

```
.
.
.
.
[Cookie 😅]
Seleniun java lib version: 3.141.59 
java version: 13.0.1
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 44 others
👎  , ,
properties (23)
post_id84,712,837
authorjune0620
permlink675gxg-java-11-web-automation-11-controlling-steemit-web-pages-with-selenium-9-scroll-kr
categoryhive-180932
json_metadata{"tags":["cn","cn-stem","mini","steemstem","zzan","dblog","java"],"image":["https:\/\/files.steempeak.com\/file\/steempeak\/june0620\/DKldjbG3-reds-java-house-1591357_1280.jpg"],"links":["https:\/\/pixabay.com\/photos\/red-s-java-house-eatery-cafe-1591357\/0"],"app":"steemit\/0.2","format":"markdown"}
created2020-02-25 12:46:15
last_update2020-02-25 12:56:15
depth0
children6
net_rshares23,678,731,017,703
last_payout2020-03-03 12:46:15
cashout_time1969-12-31 23:59:59
total_payout_value3.275 SBD
curator_payout_value2.595 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length3,143
author_reputation219,336,602,045,061
root_title"[JAVA #11] [Web Automation #11] Controlling STEEMIT web pages with Selenium #9 Scroll [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (111)
@sunai ·
虽然看不懂,也来拍拍手👏
properties (22)
post_id84,729,799
authorsunai
permlinkq6aft5
categoryhive-180932
json_metadata{"app":"busy\/2.5.4"}
created2020-02-26 02:48:42
last_update2020-02-26 02:48:42
depth1
children1
net_rshares0
last_payout2020-03-04 02:48:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length12
author_reputation82,329,514,724,848
root_title"[JAVA #11] [Web Automation #11] Controlling STEEMIT web pages with Selenium #9 Scroll [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@june0620 ·
谢谢@sunai😁
properties (22)
post_id84,731,485
authorjune0620
permlinkre-sunai-2020226t134420825z
categoryhive-180932
json_metadata{"tags":["esteem"],"app":"esteem\/2.2.4-mobile","format":"markdown+html","community":"hive-125125"}
created2020-02-26 04:44:21
last_update2020-02-26 04:44:21
depth2
children0
net_rshares0
last_payout2020-03-04 04:44:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length9
author_reputation219,336,602,045,061
root_title"[JAVA #11] [Web Automation #11] Controlling STEEMIT web pages with Selenium #9 Scroll [CN]"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@lnakuma ·
一直想学习一些UI Test。👏 👏
properties (22)
post_id84,731,368
authorlnakuma
permlinkre-june0620-2020225t213646271z
categoryhive-180932
json_metadata{"tags":["cn","cn-stem","mini","steemstem","zzan","dblog","java"],"app":"esteem\/2.2.3-surfer","format":"markdown+html","community":"esteem.app"}
created2020-02-26 04:36:48
last_update2020-02-26 04:36:48
depth1
children2
net_rshares0
last_payout2020-03-04 04:36:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length18
author_reputation29,286,445,646,252
root_title"[JAVA #11] [Web Automation #11] Controlling STEEMIT web pages with Selenium #9 Scroll [CN]"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@june0620 ·
我也不是很懂,边学边写日记 😄
properties (22)
post_id84,731,479
authorjune0620
permlinkre-lnakuma-2020226t134355745z
categoryhive-180932
json_metadata{"tags":["esteem"],"app":"esteem\/2.2.4-mobile","format":"markdown+html","community":"hive-125125"}
created2020-02-26 04:43:57
last_update2020-02-26 04:43:57
depth2
children1
net_rshares0
last_payout2020-03-04 04:43:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length16
author_reputation219,336,602,045,061
root_title"[JAVA #11] [Web Automation #11] Controlling STEEMIT web pages with Selenium #9 Scroll [CN]"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@lnakuma ·
挺好的,分享knowledge。作为读者也受益👍
properties (22)
post_id84,731,987
authorlnakuma
permlinkre-june0620-2020225t221726193z
categoryhive-180932
json_metadata{"tags":["esteem"],"app":"esteem\/2.2.3-surfer","format":"markdown+html","community":"esteem.app"}
created2020-02-26 05:17:27
last_update2020-02-26 05:17:27
depth3
children0
net_rshares0
last_payout2020-03-04 05:17:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length24
author_reputation29,286,445,646,252
root_title"[JAVA #11] [Web Automation #11] Controlling STEEMIT web pages with Selenium #9 Scroll [CN]"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@cnbuddy ·
吃了吗?你好!家中可愛的寵物照想要跟大家分享嗎?或是出去玩拍到一些可愛的動物,別忘了到@dpet分享,可以得到@dpet的獎勵喔!感谢支持。
properties (22)
post_id101,132,269
authorcnbuddy
permlinkre-june0620-675gxg-java-11-web-automation-11-controlling-steemit-web-pages-with-selenium-9-scroll-kr-20220919t052450819z
categoryhive-180932
json_metadata{}
created2022-09-19 05:24:51
last_update2022-09-19 05:24:51
depth1
children0
net_rshares0
last_payout2022-09-26 05:24:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length70
author_reputation-1,405,328,253,928
root_title"[JAVA #11] [Web Automation #11] Controlling STEEMIT web pages with Selenium #9 Scroll [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000