WebDriver自动登录BBS找到特定帖子回复指定内容领铜钱(xpath初

【环境准备】

解压缩完Eclipse,无法正常使用,提示:JVM版本过低,得1.7以上才行。好吧,之前什么时候我自己装了个1.6版本的JRE导致的。

那就重新装个最新的JDK。MAC OS装了新版JDK之后,,可以用/usr/libexec/java_home命令行查看javahome路径,我的如下:

/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home

JDK和JRE有什么区别?

有一篇博文讲的比较清楚:

接下来去官网下载需要用到的jar包

selenium-server-standalone-2.46.0.jar,这个jar是关于selenium server的

(为啥链接长这样,我也好奇~官网上这么贴的)

selenium-java-2.46.0.jar,这个jar是用来使用特定语言跟selenium server或者selenium WebDriver进行交互用的,我们用的是java,所以下载这个

【代码】

上述两个jar,在我们工程建好后,通过Build Path -> Configure Build Path -> Libraries -> Add External Jars加入到我们的工程中来

简要的说下过程:

<pre name="code" class="java">package autoLoginReply;import java.util.concurrent.TimeUnit;import java.util.Calendar;import java.util.regex.*;import org.junit.*;import static org.junit.Assert.*;import org.openqa.selenium.*;import org.openqa.selenium.firefox.FirefoxDriver;public class AutoLoginReply { private WebDriver driver; private String loginUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception {driver = new FirefoxDriver();loginUrl = "https://www.wacai.com/user/user.action?url=http%3A%2F%2Fbbs.wacai.com%2Fportal.php";driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); } @Test public void testLogin() throws Exception {//open the login pagedriver.get(loginUrl);driver.findElement(By.id("account")).clear();driver.findElement(By.id("account")).sendKeys("***");driver.findElement(By.id("pwd")).clear();driver.findElement(By.id("pwd")).sendKeys("***");driver.findElement(By.id("login-btn")).click();//get the bbs portal page, find the specific forum hrefString a = driver.findElement(By.linkText("签到有礼")).getAttribute("href");driver.navigate().to(a);//get(a);//Thread titleCalendar c = Calendar.getInstance();int m = c.get(Calendar.MONTH)+1;int d = c.get(Calendar.DAY_OF_MONTH);String dd = ""+d;if (d < 10)dd = "0"+dd;String threadTitle = "签到有礼"+m+"."+dd+"每天签到得铜钱";//System.out.println(threadTitle);//find today's check-in threada = driver.findElement(By.partialLinkText(threadTitle)).getAttribute("href");//System.out.println(a);driver.navigate().to(a);//using regrex to find the content we use to replyPattern p = Pattern.compile("回帖内容必须为.+</font>非此内容将收回铜钱奖励");Matcher r = p.matcher(driver.getPageSource().toString());StringBuffer key = new StringBuffer();while(r.find()){key.append(r.group());}//using xpath locate the textarea and submit buttondriver.findElement(By.xpath("//textarea[@id='fastpostmessage']")).sendKeys(key.substring(key.indexOf(">")+1, key.indexOf("</")-1));//driver.manage().window().maximize();//System.out.println(driver.getPageSource());driver.findElement(By.xpath("//button[@id='fastpostsubmit']")).click(); } @After public void tearDown() throws Exception {driver.quit();String verificationErrorString = verificationErrors.toString();if (!"".equals(verificationErrorString)) {fail(verificationErrorString);} } private boolean isElementPresent(By by) {try {driver.findElement(by);return true;} catch (NoSuchElementException e) {return false;} } private boolean isAlertPresent() {try {driver.switchTo().alert();return true;} catch (NoAlertPresentException e) {return false;} } private String closeAlertAndGetItsText() {try {Alert alert = driver.switchTo().alert();String alertText = alert.getText();if (acceptNextAlert) {alert.accept();} else {alert.dismiss();}return alertText;} finally {acceptNextAlert = true;} }}

【小知识点Get】

driver.navigate().to(url):跳转到指定的url,只执行跳转动作,不判断、不等待指定的页面是否加载成功;driver.get(url):跳转到指定的url,并且检查页面是否加载完毕,如果指定了timeout,而在指定时间内没有加载完毕则会抛出org.openqa.selenium.TimeoutException。

版权声明:本文为博主原创文章,未经博主允许不得转载。

所有的失败,与失去自己的失败比起来,更是微不足道

WebDriver自动登录BBS找到特定帖子回复指定内容领铜钱(xpath初

相关文章:

你感兴趣的文章:

标签云: