[JAVA 5#] [Web Automation 5#] Controlling STEEMIT web pages with Selenium #3 Actions [CN] by june0620

View this thread on steempeak.com
· @june0620 ·
$5.56
[JAVA 5#] [Web Automation 5#] Controlling STEEMIT web pages with Selenium #3 Actions [CN]
![redsjavahouse1591357_1280.jpg](https://steemitimages.com/0x0/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)

---

接下来我们给元素加点action吧。除上期使用过的click()外详细的再探索一下别的action。
首先呢,用sendKeys(), click(), submit()函数登录STEEMIT并写贴。当然,不点击保存按钮 哈哈(希望在不远的将来AI帮我们写贴)

#### 分析

1. 用 By.linkText()获取登录按钮并 click()
2. By.name()可以查找用户名及密码输入框并用 sendKeys()输入文本
3. 登录部分都包含在form元素,所以可以用 submit()来简单提交
4. 搜索了撰写按钮,发现有两按钮,一个显示在页面,另一个不显示。那么可以用 isDisplayed()来判断按钮。
5. 用 sendKeys()往编辑器输入文本。**一般的 String和 Selenium提供的 Keys enum都可以用到参数。如果想加两个以上键盘按键则可以用 Keys.chord()。再一个,参数为可变参数,可以输入多个参数。**

```
package com.steem.webatuo;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

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/@june0620/feed");
 driver.manage().window().maximize();
 driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
 
 //Click the login button
 driver.findElement(By.linkText("로그인")).click();
 // type id
 driver.findElement(By.name("username")).sendKeys("june0620");
 // type pw and submit
 WebElement pw = driver.findElement(By.name("password"));
 pw.sendKeys(Password.getPw());
 pw.submit();
 Thread.sleep(5000);
 // Click the write Button 
 List<WebElement> writeBtns = driver.findElements(By.xpath("//a[@href='/submit.html']"));
 for(WebElement writeBtn : writeBtns) {
 if(writeBtn.isDisplayed()) {
 writeBtn.click();
 Thread.sleep(2000);
 //type Text and Keys
 WebElement editor = driver.findElement(By.xpath("//textarea[@name='body']"));
 String keys = Keys.chord(Keys.LEFT_SHIFT, Keys.ENTER);
 editor.sendKeys("hello!! world", keys, "hello!!! STEEMIT", Keys.ENTER, "안녕, 스팀잇", Keys.ENTER, "你好!似提姆");
 break;
 }
 }
 
 Thread.sleep(5000);
 driver.quit();
 }

}

```

#### 视频:

[https://youtu.be/FVL4VsU6qiM](https://youtu.be/FVL4VsU6qiM)

#### ETC.

selenium 的 Actions 类可以给action添加更多的色彩。
Actions 类包括 clickAndHold, contextClick, doubleClick, dragAndDrop, moveToElemen 等函数。👇
![image.png](https://steemitimages.com/0x0/https://files.steempeak.com/file/steempeak/june0620/o9WiBWKq-image.png)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 37 others
👎  
properties (23)
post_id83,866,061
authorjune0620
permlinkjava-5-web-automation-5-controlling-steemit-web-pages-with-selenium-3-actions-cn
categorycn
json_metadata{"app":"steempeak\/2.2.8","format":"markdown","tags":["cn","cn-stem","mini","steemstem","zzan","palnet","liv","lifestyle","dblog","java"],"users":["june0620","href","name"],"links":["https:\/\/pixabay.com\/photos\/red-s-java-house-eatery-cafe-1591357\/0","https:\/\/youtu.be\/FVL4VsU6qiM"],"image":["https:\/\/files.steempeak.com\/file\/steempeak\/june0620\/DKldjbG3-reds-java-house-1591357_1280.jpg","https:\/\/files.steempeak.com\/file\/steempeak\/june0620\/o9WiBWKq-image.png"]}
created2020-01-29 03:41:18
last_update2020-01-29 03:41:18
depth0
children4
net_rshares22,468,140,471,647
last_payout2020-02-05 03:41:18
cashout_time1969-12-31 23:59:59
total_payout_value3.064 SBD
curator_payout_value2.492 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length2,533
author_reputation219,336,602,045,061
root_title"[JAVA 5#] [Web Automation 5#] Controlling STEEMIT web pages with Selenium #3 Actions [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (102)
@cnbuddy ·
你今天过的开心吗?[欢迎在steemauto里设置跟赞 @cnbuddy](https://steemit.com/cn/@shenchensucc/6y3bap-cnbuddy-cn) 给整个cn区点赞倘若你想让我隐形,请回复“取消”。
properties (22)
post_id83,866,339
authorcnbuddy
permlinkre-june0620-java-5-web-automation-5-controlling-steemit-web-pages-with-selenium-3-actions-cn-20200129t035802798z
categorycn
json_metadata{}
created2020-01-29 03:58:03
last_update2020-01-29 03:58:03
depth1
children0
net_rshares0
last_payout2020-02-05 03:58:03
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_length119
author_reputation-1,405,328,253,928
root_title"[JAVA 5#] [Web Automation 5#] Controlling STEEMIT web pages with Selenium #3 Actions [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@steemitboard ·
Congratulations @june0620! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://steemitimages.com/60x70/http://steemitboard.com/@june0620/votes.png?202001290612"></td><td>You distributed more than 16000 upvotes. Your next target is to reach 17000 upvotes.</td></tr>
</table>

<sub>_You can view [your badges on your Steem Board](https://steemitboard.com/@june0620) and compare to others on the [Steem Ranking](https://steemitboard.com/ranking/index.php?name=june0620)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>


To support your work, I also upvoted your post!


###### [Vote for @Steemitboard as a witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1) to get one more award and increased upvotes!
properties (22)
post_id83,869,161
authorsteemitboard
permlinksteemitboard-notify-june0620-20200129t070202000z
categorycn
json_metadata{"image":["https:\/\/steemitboard.com\/img\/notify.png"]}
created2020-01-29 07:02:03
last_update2020-01-29 07:02:03
depth1
children0
net_rshares0
last_payout2020-02-05 07:02:03
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_length899
author_reputation38,705,954,145,809
root_title"[JAVA 5#] [Web Automation 5#] Controlling STEEMIT web pages with Selenium #3 Actions [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@dailychina ·
$0.05
!thumbup
  恭喜你!您的这篇文章入选 @justyy 今日 (2020-01-30) 榜单 [【优秀的文章】](https://steemit.com/cn/@justyy/--daily-cn-updates-cncnpower-downyy2020-01-30), 回复本条评论24小时内领赏,点赞本评论将支持 @dailychina 并增加将来您的奖赏。
 @justyy 是CN区的见证人,[请支持他,给他投票](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=justyy),或者设置justyy为[见证人代理](https://v2.steemconnect.com/sign/account-witness-proxy?proxy=justyy&approve=1)。感谢!@justyy的主要贡献:https://steemyy.com

Congratulations! This post has been selected by @justyy as today's (2020-01-30)  [【Good Posts】](https://steemit.com/cn/@justyy/--daily-cn-updates-cncnpower-downyy2020-01-30), Steem On!  Reply to this message in 24 hours to get rewards. Upvote this comment to support the @dailychina and increase your future rewards! ^_^

[SteemIt 工具、API接口、机器人和教程](https://steemyy.com/steemit-tools/)
[SteemIt Tools, Bots, APIs and Tutorial](https://SteemYY.com)<hr>
If you believe what I am doing, please consider a spare vote voting me [here](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=justyy), thank you very much indeed.

@justyy - the author of https://SteemYY.com and I have been a Steem Witness for [more than a year now.](https://steemit.com/witness-category/@justyy/one-year-winessversary-a-great-start)
👍  , , , , , , , , , , ,
properties (23)
post_id83,909,179
authordailychina
permlinkre-java-5-web-automation-5-controlling-steemit-web-pages-with-selenium-3-actions-cn-20200130t080159
categorycn
json_metadata{}
created2020-01-30 08:02:00
last_update2020-01-30 08:02:00
depth1
children1
net_rshares307,700,126,723
last_payout2020-02-06 08:02:00
cashout_time1969-12-31 23:59:59
total_payout_value0.024 SBD
curator_payout_value0.023 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length1,249
author_reputation50,118,723,362,727
root_title"[JAVA 5#] [Web Automation 5#] Controlling STEEMIT web pages with Selenium #3 Actions [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (12)
@june0620 ·
$0.05
properties (23)
post_id83,909,240
authorjune0620
permlinkre-dailychina-2020130t1755543z
categorycn
json_metadata{"tags":["esteem"],"app":"esteem\/2.2.3-mobile","format":"markdown+html","community":"esteem.app"}
created2020-01-30 08:05:54
last_update2020-01-30 08:05:54
depth2
children0
net_rshares318,470,371,596
last_payout2020-02-06 08:05:54
cashout_time1969-12-31 23:59:59
total_payout_value0.025 SBD
curator_payout_value0.024 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length3
author_reputation219,336,602,045,061
root_title"[JAVA 5#] [Web Automation 5#] Controlling STEEMIT web pages with Selenium #3 Actions [CN]"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (12)