java脚本,selenium工具,自动发QQ邮件

package com.sele.test;
import com.thoughtworks.selenium.*;
import org.openqa.selenium.By;


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.By.ById;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.browserlaunchers.locators.GoogleChromeLocator;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class seleTest {

public static void main(String[] args) throws Exception {

// 用火狐浏览器
// System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
// WebDriver driver = new FirefoxDriver();

// 用谷歌浏览器
System.setProperty("webdriver.chrome.driver", "C:\\Users\\THINK\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// WebDriver drivertemp=null;
// go to url
driver.get("https://mail.qq.com/cgi-bin/loginpage?autologin=n&errtype=1&verify=&clientuin=&t=&alias=&regalias=&delegate_url=&title=&url=%2Fcgi-bin%2Flogin%3Fvt%3Dpassport%26vm%3Dwpt%26ft%3Dloginpage%26target%3D&org_fun=&aliastype=&ss=&from=&param=&sp=&r=b6599bedbb40de77b92f1f8ed69c31f4&ppp=&secpp=&tfcont=22%20serialization%3A%3Aarchive%205%200%200%204%200%200%200%208%20authtype%201%204%206%20domain%206%20qq.com%202%20vm%203%20wpt%202%20ft%209%20loginpage");

//进入ifram框架
WebElement element = null;
// driver.findElement(By.id("login_frame"));
driver.switchTo().frame("login_frame");

//定位iframe的元素账号密码登录
driver.findElement(By.linkText("帐号密码登录")).click();
driver.findElement(By.id("u")).sendKeys("");//账号
driver.findElement(By.id("p")).sendKeys("");//密码
driver.findElement(By.id("login_button")).click();
driver.switchTo().defaultContent();
//设置等待时间
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//点写信
//设置等待时间
// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(1000);
element = driver.findElement(By.id("composebtn"));
System.out.println(element.getText());
element.click();

//进入写信

//等待页面加载
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//验证点击的是 写信,
String strEleText = element.getText();
System.out.println(element.getText());
System.out.println("点击写信后的网址"+driver.getCurrentUrl());
System.out.println(element.getAttribute("id"));
// element.click();
System.out.println("=====================");

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
WebDriver drivertemp=null;
drivertemp = driver.switchTo().frame("mainFrame");
System.out.println("drivertemp switch到mainframe后的网址"+drivertemp.getCurrentUrl());
System.out.println("driver 的网址"+driver.getCurrentUrl());
//点击普通邮件
element = driver.findElement(By.xpath("html/body/form[2]/table/tbody/tr/td[1]/div[2]/div"));
// element.click();
System.out.println(element.getText());

//用收件人来验证是不是普通邮件
element = driver.findElement(By.id("to_btn"));
System.out.println(element.getText());

//输入收件人
element= driver.findElement(By.xpath("html/body/form[2]/div[2]/div[3]/div[2]/table[2]/tbody/tr/td[2]/div[1]/div[2]/input"));
System.out.println(element.getAttribute("class"));
element.sendKeys("收件人");
System.out.println(element.getAttribute("value"));


//等待页面加载
driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS);

//输入主题
element = driver.findElement(By.id("subject"));
element.sendKeys("测试");
System.out.println(element.getAttribute("value"));

//等待页面加载
driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS);
Thread.sleep(2000);
//输入正文
// drivertemp = driver.switchTo().frame("mainFrame");
System.out.println(drivertemp.findElement(By.tagName("iframe")).getTagName());
// driver.switchTo().frame("//iframe[@id='_152143094072908127512924263665'][@class='qmEditorIfrmEditArea']");
// driver.switchTo().frame("//*[@id='_152143629523306963933688306077'");
element = drivertemp.findElement(By.tagName("iframe"));
System.out.println(element.getAttribute("class"));

//切换页面到正文收入框
// driver.switchTo().frame(element);
driver.switchTo().frame(drivertemp.findElement(By.tagName("iframe")));//曾提示所定位到的元素提示元素失效,我也不知道怎么解决,但是调来调去,就正常了,真不知道怎么回事
//输入正文
driver.findElement(By.tagName("body")).sendKeys("自动发送第一封信,谢谢查看");
// driver.switchTo().frame("mainFrame");
// driver.switchTo().frame("mainFrame");
driver.switchTo().parentFrame();
driver.findElement(By.linkText("发送")).click();
Thread.sleep(5000);
// driver.quit();

}

}

 

 

 

 

总结:

 1、driver.switchTo().frame("mainFrame")这个函数只能像子框架跳转,而不能向父框架跳,而且只能一级一级跳

       跳往父框架只能用driver.switchTo().parentFrame();

2、当火狐有拦截插件的功能时,会经常 弹不出内联框架,从而不能找到所定位的元素

3、对于所定位到的元素提示元素失效,我也不知道怎么解决,但是调来调去,就正常了,真不知道怎么回事

posted @ 2018-03-20 18:18  满塘荷香  阅读(218)  评论(0编辑  收藏  举报