java Selenium 發送126郵件 解決element not visible__Selenium

來源:互聯網
上載者:User

網上很多例子,但是到了最後一次發送郵件的時候出現 element not visible 錯誤,因為頁面中有兩個發送按鈕,  幾經周折, 終於搞定了。

 

 

package selenium_java;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Login126Email {
 static WebDriver driver = new ChromeDriver();

 public static void main(String[] args) throws InterruptedException {
  login_126_email(); 
  send_126_email();
  //driver.close();  
 }

 

 public static void login_126_email() throws InterruptedException{
  System.setProperty("webdriver.chrome.driver",
    "D:\\install\\selenium\\chromedriver2.25_win32\\chromedriver.exe");

  driver.get("http://mail.126.com/");
  Thread.sleep(1000);//必須等

  //登入
  driver.switchTo().frame(0);
  driver.findElement(By.name("email")).clear();
  driver.findElement(By.name("email")).sendKeys("testselenium");

  driver.findElement(By.name("password")).clear();
  driver.findElement(By.name("password")).sendKeys("!@#$%^");
  driver.findElement(By.id("dologin")).click();
  Thread.sleep(1000);
 }


 public static void send_126_email() throws InterruptedException{
  //  寫信
  WebElement xiexin_element = driver.findElement(By.xpath("//span[contains(.,'寫 信')]"));
  xiexin_element.click();
  Thread.sleep(1000);

  WebElement shoujianren_element = driver.findElement(By.className("nui-editableAddr-ipt"));
  shoujianren_element.sendKeys("washli2001@126.com");

  //WebElement zhuti_element = driver.findElement(By.xpath("//input[@class='nui-ipt-input'][contains(@id,'_subjectInput')]"));//以下一個過濾提交愛哦即可
  WebElement zhuti_element = driver.findElement(By.xpath("//input[contains(@id,'_subjectInput')]"));
  zhuti_element.sendKeys("Selenium自動發送126郵件測試");

  WebElement youjianneirong_element = driver.switchTo().frame(driver.findElement(By.className("APP-editor-iframe"))).findElement(By.className("nui-scroll"));
  youjianneirong_element.sendKeys("你好。這裡是測試郵件,請您忽略。 謝謝。  發送郵箱內容。。。");
  driver.switchTo().defaultContent();

  Actions actions = new Actions(driver);
  List<WebElement>  spanss = driver.findElements(By.xpath("//span[contains(.,'發送')]"));//因為有兩個發送按鈕
  for (WebElement span : spanss){
   actions.moveToElement(span).click().build().perform();
  }

 

  //退出登入
  WebElement tuichu_element = driver.findElement(By.linkText("退出"));
  tuichu_element.click();

 }

}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.