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

in #cn4 years ago

redsjavahouse1591357_1280.jpg
image source: pixabay


今天介绍以下功能非常强大的库Selenium。先简单的写下代码,等以后再结合testNG,Jenkins。
create1.png
👆 1. 在上期创建的项目的 src/main/java上创建一个package。 package 名一般都命名为反url。真不知道为什么这么命名,但没必要计较了。

create2.png
👆 2. 在 package 再创建一个 Class。Class命名规矩是第一个字母大写的名次。就写Steemit并打勾下面的public static void main(String[] args)。这样会自动生成java的main代码,在这里写下控制浏览器的代码即可。

create3.png
👆 3. 那,下一步要下载selenium库了,可以访问这里下载。
https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
会显示版本目录,都4.0 alpha都有了? 话说alpha版本有点危险,下载稳定版本3.x吧。 点击进入3.x版本把maven下的代码拷贝。

create4.png
👆 4. 回到 IDE 打开项目的根目录的 pom.xml 文件并在xml模式在 元素下输入 <dependencies>,</dependencies>

create5.png
👆 5. 在这两个元素间输入刚才拷贝的代码,这样Elipse会自动下载相关库到本地,下载速度有点慢, 不着急边编辑图片边等吧。

create6.png
👆 6. 下载完项目左侧会显示Maven Dependancies 库。如图。

create7.png
👆 7. 用同样的方法再下载 WebDriverManager 库,虽然没有必要下载这个,但有这个库会节省很多航代码,维护起来也更方便。访问这里可下载。
https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager/3.7.1

create8.png
👆 8. 回到step2在main代码输入需要的代码就OK了。

分析代码
8-1. 设置 chromedriver。(也可以设置火狐等其他浏览器)
8-2. 创建 Chromedriver 对象。
8-3. 打开浏览器并移动到https://steemit.com/@june0620/feed
8-4. 设置窗口大小为最大
8-5. 在页面获取대세글元素点击后等待2秒。
8-6. 关闭浏览器。

package com.steem.webatuo;

import org.openqa.selenium.By;
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();
 WebElement trending = driver.findElement(By.linkText("대세글"));
 trending.click();
 Thread.sleep(2000);
 
 driver.quit();
 }

}
视频演示:

https://youtu.be/1e8sZIJMZJY

Sort:  

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

Congratulations! This post has been selected by @justyy as today's (2020-01-22) 【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.

@tipu curate
Hi又巧啦😁

Posted using Partiko Android

哈哈😄 多谢tipu

Posted using Partiko Android

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

You received more than 15000 upvotes. Your next target is to reach 20000 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

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

Coin Marketplace

STEEM 0.25
TRX 0.11
JST 0.032
BTC 63134.74
ETH 3052.94
USDT 1.00
SBD 3.81