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

in #cn4 years ago

redsjavahouse1591357_1280.jpg
image source: pixabay


接下来我们给元素加点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

ETC.

selenium 的 Actions 类可以给action添加更多的色彩。
Actions 类包括 clickAndHold, contextClick, doubleClick, dragAndDrop, moveToElemen 等函数。👇
image.png

Sort:  

!thumbup
恭喜你!您的这篇文章入选 @justyy 今日 (2020-01-30) 榜单 【优秀的文章】, 回复本条评论24小时内领赏,点赞本评论将支持 @dailychina 并增加将来您的奖赏。
@justyy 是CN区的见证人,请支持他,给他投票,或者设置justyy为见证人代理。感谢!@justyy的主要贡献:https://steemyy.com

Congratulations! This post has been selected by @justyy as today's (2020-01-30) 【Good Posts】, 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接口、机器人和教程
SteemIt Tools, Bots, APIs and Tutorial



If you believe what I am doing, please consider a spare vote voting me here, 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.

你今天过的开心吗?欢迎在steemauto里设置跟赞 @cnbuddy 给整个cn区点赞倘若你想让我隐形,请回复“取消”。

Congratulations @june0620! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You distributed more than 16000 upvotes. Your next target is to reach 17000 upvotes.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.18
TRX 0.14
JST 0.029
BTC 58068.07
ETH 3133.85
USDT 1.00
SBD 2.44