![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
post_id | 84,712,837 |
---|---|
author | june0620 |
permlink | 675gxg-java-11-web-automation-11-controlling-steemit-web-pages-with-selenium-9-scroll-kr |
category | hive-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"} |
created | 2020-02-25 12:46:15 |
last_update | 2020-02-25 12:56:15 |
depth | 0 |
children | 6 |
net_rshares | 23,678,731,017,703 |
last_payout | 2020-03-03 12:46:15 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 3.275 SBD |
curator_payout_value | 2.595 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 3,143 |
author_reputation | 219,336,602,045,061 |
root_title | "[JAVA #11] [Web Automation #11] Controlling STEEMIT web pages with Selenium #9 Scroll [CN]" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
gerber | 0 | 71,950,804,830 | 3% | ||
daan | 0 | 189,168,438,301 | 3% | ||
slowwalker | 0 | 44,714,095,932 | 7% | ||
exyle | 0 | 414,806,063,670 | 5% | ||
magicmonk | 0 | 1,567,592,999,274 | 100% | ||
tumutanzi | 0 | 3,487,119,747 | 50% | ||
oldstone | 0 | 102,667,291,128 | 5% | ||
petrvl | 0 | 10,711,447,155 | 1.5% | ||
coldhair | 0 | 892,445,338 | 50% | ||
lotusofmymom | 0 | 4,368,857,915 | 3.5% | ||
rycharde | 0 | 1,088,669,294 | 2.7% | ||
allsale | 0 | 393,312,974,961 | 100% | ||
belahejna | 0 | 1,793,953,526 | 1.5% | ||
virus707 | 0 | 2,962,900,128,934 | 19% | ||
shenchensucc | 0 | 141,616,987,962 | 30% | ||
krischy | 0 | 41,608,937,243 | 70% | ||
ew-and-patterns | 0 | 139,164,562,025 | 8% | ||
techken | 0 | 0 | 0.21% | ||
khaiyoui | 0 | 972,625,832,819 | 25% | ||
christianity101 | 0 | 259,977,334 | 66% | ||
ilovemylife | 0 | 734,456,845,604 | 32.5% | ||
nicolemoker | 0 | 78,559,731,976 | 60% | ||
azulear | 0 | 5,237,646,019 | 100% | ||
kimzwarch | 0 | 8,029,723,016 | 4% | ||
accelerator | 0 | 41,807,085,811 | 2.7% | ||
june0620 | 0 | 1,144,846,703,584 | 100% | ||
minloulou | 0 | 2,326,656,501 | 10% | ||
lindalex | 0 | 580,290,767 | 50% | ||
blockbrothers | 0 | 64,584,364,524 | 2.5% | ||
rudyardcatling | 0 | 3,393,813,142 | 100% | ||
cnbuddy | 0 | 1,610,597,187,054 | 100% | ||
banjjakism | 0 | 6,017,520,093 | 9.5% | ||
itchyfeetdonica | 0 | 412,652,559,816 | 100% | ||
funtraveller | 0 | 17,098,910,524 | 3% | ||
stupid | 0 | 618,093,994 | 40% | ||
mehta | 0 | 66,258,994,220 | 40% | ||
suhunter | 0 | 940,069,843 | 50% | ||
piotrgrafik | 0 | 48,147,204,175 | 15% | ||
udabeu | 0 | 12,270,755,889 | 30% | ||
marsswim | 0 | 108,597,607,114 | 51% | ||
cadawg | 0 | 10,067,879,316 | 2.1% | ||
photoholic | 0 | 324,010,109,677 | 7% | ||
also.einstein | 0 | 6,989,682,617 | 100% | ||
earlmonk | 0 | 576,890,764 | 5% | ||
bestboom | 0 | 23,007,018,116 | 3% | ||
lucky2015 | 0 | 333,542,448,448 | 100% | ||
gghite | 0 | 416,859,210,261 | 65% | ||
jaydih | 0 | 301,586,035,478 | 11% | ||
andrewma | 0 | 11,054,703,580 | 50% | ||
wisdomandjustice | 0 | 118,633,867,385 | 5% | ||
ravenkim | 0 | 9,876,704,450 | 100% | ||
pilimili | 0 | 459,834,860 | 95% | ||
wanggang | 0 | 151,003,852,937 | 20% | ||
pagliozzo | 0 | 17,822,768,389 | 20% | ||
harkar | 0 | 87,646,877,146 | 20% | ||
dlike | 0 | 157,679,998,483 | 2.2% | ||
triptolemus | 0 | 12,061,215,946 | 3% | ||
melaniewang | 0 | 8,066,987,875 | 50% | ||
changxiu | 0 | 4,864,321,647 | 50% | ||
laissez-faire | 0 | 117,773,318 | 100% | ||
pet.society | 0 | 15,807,500,647 | 5% | ||
cherryzz | 0 | 182,603,606,716 | 50% | ||
djusti | 0 | 1,526,374,965,575 | 100% | ||
nineteensixteen | 0 | 672,456,278,017 | 100% | ||
wherein | 0 | 2,041,017,563,092 | 48% | ||
sindong | 0 | 652,636,615,703 | 80% | ||
steemfriends | 0 | 4,243,843,551 | 100% | ||
cyberrn | 0 | 855,065,731,496 | 19% | ||
permaculturedude | 0 | 544,764,588 | 1.5% | ||
tina3721 | 0 | 4,865,573,237 | 50% | ||
andyhsia | 0 | 6,654,530,476 | 100% | ||
minigame | 0 | 1,806,442,125,530 | 5.2% | ||
jjm13 | 0 | 17,740,183,338 | 19% | ||
mustard-seed | 0 | 25,206,295,124 | 11% | ||
fsm-liquid | 0 | 13,412,760,691 | 5% | ||
anthos | 0 | 1,835,387,963 | 5% | ||
zzan.co3 | 0 | 574,085,701 | 100% | ||
aaronli.sct | 0 | 689,209,694 | 10% | ||
oldstone.sct | 0 | 139,762,934,710 | 7% | ||
steem-drivers | 0 | 3,873,811,585 | 34% | ||
triplea.bot | 0 | 1,634,165,212 | 5% | ||
steem.leo | 0 | 87,492,656,571 | 2.85% | ||
cn-zzang | 0 | 1,235,287,708 | 5% | ||
perrymine | 0 | 885,590,955 | 11% | ||
freddio.sport | 0 | 41,582,049,594 | 15% | ||
mapxv | 0 | 17,590,776,014 | 2.16% | ||
leo.syndication | 0 | 1,418,513,906 | 3% | ||
one.life | 0 | 30,976,727,015 | 2.99% | ||
maxuva | 0 | 1,960,110,148 | 2.7% | ||
maxuvb | 0 | 1,256,351,871 | 2.7% | ||
maxuvc | 0 | 1,261,609,645 | 2.7% | ||
maxuvd | 0 | 1,966,939,279 | 2.7% | ||
maxuve | 0 | 9,629,724,298 | 2.16% | ||
cnbuddy-reward | 0 | 801,653,482,417 | 100% | ||
roseofmylife | 0 | 100,094,783,279 | 7% | ||
springflower | 0 | 245,172,769,419 | 7% | ||
mychoco | 0 | 120,702,119,352 | 7% | ||
morningcare | 0 | 2,796,341,693 | 5% | ||
real3earch | 0 | 7,891,948,288 | 100% | ||
unpopular | 0 | 28,630,639,825 | 40% | ||
kryptoformator | 0 | 641,696,226 | 0.75% | ||
we-together | 0 | 371,159,478,920 | 7% | ||
steem-agora | 0 | 221,444,046,674 | 7% | ||
dblogvoter | 0 | 80,328,724 | 1.95% | ||
reward.tier2 | 0 | 1,636,314,998 | 100% | ||
p-translation | 0 | 159,335,801,612 | 7% | ||
bilpcoinpower | 0 | 208,833,918 | 10% | ||
ovilator | 0 | 0 | -10% | ||
tzimane | 0 | -5,921,886 | -10% | ||
steem.idols | 0 | 18,917,396,240 | 3% | ||
councoun | 0 | -8,843,393 | -10% |
虽然看不懂,也来拍拍手👏
post_id | 84,729,799 |
---|---|
author | sunai |
permlink | q6aft5 |
category | hive-180932 |
json_metadata | {"app":"busy\/2.5.4"} |
created | 2020-02-26 02:48:42 |
last_update | 2020-02-26 02:48:42 |
depth | 1 |
children | 1 |
net_rshares | 0 |
last_payout | 2020-03-04 02:48:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 12 |
author_reputation | 82,329,514,724,848 |
root_title | "[JAVA #11] [Web Automation #11] Controlling STEEMIT web pages with Selenium #9 Scroll [CN]" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
谢谢@sunai😁
post_id | 84,731,485 | ||||||
---|---|---|---|---|---|---|---|
author | june0620 | ||||||
permlink | re-sunai-2020226t134420825z | ||||||
category | hive-180932 | ||||||
json_metadata | {"tags":["esteem"],"app":"esteem\/2.2.4-mobile","format":"markdown+html","community":"hive-125125"} | ||||||
created | 2020-02-26 04:44:21 | ||||||
last_update | 2020-02-26 04:44:21 | ||||||
depth | 2 | ||||||
children | 0 | ||||||
net_rshares | 0 | ||||||
last_payout | 2020-03-04 04:44:21 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 0.000 SBD | ||||||
curator_payout_value | 0.000 SBD | ||||||
pending_payout_value | 0.000 SBD | ||||||
promoted | 0.000 SBD | ||||||
body_length | 9 | ||||||
author_reputation | 219,336,602,045,061 | ||||||
root_title | "[JAVA #11] [Web Automation #11] Controlling STEEMIT web pages with Selenium #9 Scroll [CN]" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 SBD | ||||||
percent_steem_dollars | 10,000 |
一直想学习一些UI Test。👏 👏
post_id | 84,731,368 | ||||||
---|---|---|---|---|---|---|---|
author | lnakuma | ||||||
permlink | re-june0620-2020225t213646271z | ||||||
category | hive-180932 | ||||||
json_metadata | {"tags":["cn","cn-stem","mini","steemstem","zzan","dblog","java"],"app":"esteem\/2.2.3-surfer","format":"markdown+html","community":"esteem.app"} | ||||||
created | 2020-02-26 04:36:48 | ||||||
last_update | 2020-02-26 04:36:48 | ||||||
depth | 1 | ||||||
children | 2 | ||||||
net_rshares | 0 | ||||||
last_payout | 2020-03-04 04:36:51 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 0.000 SBD | ||||||
curator_payout_value | 0.000 SBD | ||||||
pending_payout_value | 0.000 SBD | ||||||
promoted | 0.000 SBD | ||||||
body_length | 18 | ||||||
author_reputation | 29,286,445,646,252 | ||||||
root_title | "[JAVA #11] [Web Automation #11] Controlling STEEMIT web pages with Selenium #9 Scroll [CN]" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 SBD | ||||||
percent_steem_dollars | 10,000 |
我也不是很懂,边学边写日记 😄
post_id | 84,731,479 | ||||||
---|---|---|---|---|---|---|---|
author | june0620 | ||||||
permlink | re-lnakuma-2020226t134355745z | ||||||
category | hive-180932 | ||||||
json_metadata | {"tags":["esteem"],"app":"esteem\/2.2.4-mobile","format":"markdown+html","community":"hive-125125"} | ||||||
created | 2020-02-26 04:43:57 | ||||||
last_update | 2020-02-26 04:43:57 | ||||||
depth | 2 | ||||||
children | 1 | ||||||
net_rshares | 0 | ||||||
last_payout | 2020-03-04 04:43:57 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 0.000 SBD | ||||||
curator_payout_value | 0.000 SBD | ||||||
pending_payout_value | 0.000 SBD | ||||||
promoted | 0.000 SBD | ||||||
body_length | 16 | ||||||
author_reputation | 219,336,602,045,061 | ||||||
root_title | "[JAVA #11] [Web Automation #11] Controlling STEEMIT web pages with Selenium #9 Scroll [CN]" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 SBD | ||||||
percent_steem_dollars | 10,000 |
挺好的,分享knowledge。作为读者也受益👍
post_id | 84,731,987 | ||||||
---|---|---|---|---|---|---|---|
author | lnakuma | ||||||
permlink | re-june0620-2020225t221726193z | ||||||
category | hive-180932 | ||||||
json_metadata | {"tags":["esteem"],"app":"esteem\/2.2.3-surfer","format":"markdown+html","community":"esteem.app"} | ||||||
created | 2020-02-26 05:17:27 | ||||||
last_update | 2020-02-26 05:17:27 | ||||||
depth | 3 | ||||||
children | 0 | ||||||
net_rshares | 0 | ||||||
last_payout | 2020-03-04 05:17:27 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 0.000 SBD | ||||||
curator_payout_value | 0.000 SBD | ||||||
pending_payout_value | 0.000 SBD | ||||||
promoted | 0.000 SBD | ||||||
body_length | 24 | ||||||
author_reputation | 29,286,445,646,252 | ||||||
root_title | "[JAVA #11] [Web Automation #11] Controlling STEEMIT web pages with Selenium #9 Scroll [CN]" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 SBD | ||||||
percent_steem_dollars | 10,000 |
吃了吗?你好!家中可愛的寵物照想要跟大家分享嗎?或是出去玩拍到一些可愛的動物,別忘了到@dpet分享,可以得到@dpet的獎勵喔!感谢支持。
post_id | 101,132,269 |
---|---|
author | cnbuddy |
permlink | re-june0620-675gxg-java-11-web-automation-11-controlling-steemit-web-pages-with-selenium-9-scroll-kr-20220919t052450819z |
category | hive-180932 |
json_metadata | {} |
created | 2022-09-19 05:24:51 |
last_update | 2022-09-19 05:24:51 |
depth | 1 |
children | 0 |
net_rshares | 0 |
last_payout | 2022-09-26 05:24:51 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 70 |
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_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |