2015-07-05 41 views
2

這就是我想要做的Gmail。無法點擊Gmail中的設置圖標與硒網絡驅動程序

  1. www.gmail.com使用有效的用戶(如果不存在,創建一個虛擬用戶)
  2. 登錄
  3. 點擊收件箱
  4. 點擊第一封電子郵件
  5. 單擊編寫電子郵件。
  6. 發送郵件到同一個電子郵件帳戶。
  7. 點擊選項圖標右上角
  8. 了對一般外出回覆設置選項卡
  9. 選擇外出回覆上。

我可以點擊第一封電子郵件,選擇電子郵件地址,然後點擊撰寫電子郵件按鈕,也可以發送電子郵件。 我面臨的問題是無法點擊設置圖標。該元素被隱藏,我無法點擊它。我用custom-Xpath嘗試了它,並試圖用座標點擊它。 但它不適合我。請任何人都可以幫我解決這個問題。

import org.openqa.selenium.By; 
import org.openqa.selenium.Point; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.interactions.Actions; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 


    public class Gmail1 { 

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

      // Login to browser 
      WebDriver driver= new FirefoxDriver(); 
      driver.get("https://www.gmail.com"); 
      driver.manage().window().maximize(); 
      System.out.println("Browser opned"); 
      driver.findElement(By.xpath("//*[@id='Email']")).sendKeys("Use your UserId"); 
      System.out.println("Entered Email id"); 
      driver.findElement(By.xpath("//*[@id='next']")).click(); 
      System.out.println("Clicked on Next"); 
       Thread.sleep(3000); 
      driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("Use your password"); 
      System.out.println("Entered the Password"); 
      driver.findElement(By.xpath("//*[@id='signIn']")).click(); 
      System.out.println("Welcome to gmail"); 
       Thread.sleep(5000); 
      driver.findElement(By.xpath("//*[@id=':3d']")).click(); 
      System.out.println("Clicked on email"); 
       Thread.sleep(3000); 
      String emailid = driver.findElement(By.xpath("//span[@class='go']")).getText(); 
      emailid=emailid.substring(emailid.indexOf("<")+1, emailid.indexOf(">")); 
      System.out.println(emailid); 
      driver.findElement(By.xpath("//*[@id=':it']/div/div")).click(); 
      System.out.println("Clicked on Compose mail"); 
      driver.findElement(By.xpath("//*[@name='to']")).sendKeys(emailid); 
      System.out.println("Entered the TO Email Address"); 
      driver.findElement(By.xpath("//*[@name='subjectbox']")).sendKeys("My Mail"); 
      System.out.println("Entered Subject of the email"); 
      driver.findElement(By.xpath("//*[@role='button' and .='Send']")).click(); 
      System.out.println("Clicked on send button"); 

      clickSetting(driver);  
     } 
    public static void clickSetting(WebDriver driver){ 
      //Tried with Coordinates (doesn't work) 
      Point point = driver.findElement(By.xpath("//div[@class='G-Ni J-J5-Ji'] [@gh ='s']/*[1]")).getLocation(); 
      System.out.println(point.x + "-" + point.y); 
      Actions builder = new Actions(driver); 
      builder.moveByOffset(point.x, point.y).click().build().perform(); //Getting Error. 

      //Tried with Action Class (doesn't work) 
      WebDriverWait wait = new WebDriverWait(driver, 10); 
      WebElement SettingWheel=driver.findElement(By.xpath("//*[@data-tooltip='Settings' and @role='button']")); 
      WebElement SettingsLink=driver.findElement(By.xpath("//*[@role='menuitem']/div[.='Settings']")); 
      wait.until(ExpectedConditions.elementToBeClickable(SettingWheel)); 
      Actions actions = new Actions(driver); 
      actions.moveToElement(SettingWheel).moveToElement(SettingsLink).click().build().perform();//Getting Error. 
      Thread.sleep(2000); 
      System.out.println("Clicked On Setting");   
     } 

錯誤消息: - 「元素當前不可見,因此可能無法與之交互(警告:服務器未提供任何信息棧跟蹤)」

在此先感謝。

+0

前往另一個網站。遠離谷歌 – Saifur

+0

我沒有選擇在其他網站嘗試它。我只需要在Gmail中執行此操作。請幫助我。 –

回答

1

試試這個: -

public static void clickSetting(WebDriver driver){ 
      List<WebElement> elements=driver.findElements(By.xpath("//div[@gh='s']/*[@role='button']")); 
     for(WebElement element:elements){ 
      if(element.isDisplayed()){ 
       element.click(); 
       Thread.sleep(2000); 
       driver.findElement(By.xpath("//*[@id='ms']")).click(); 
       Thread.sleep(5000); 
      }   
     } 
+0

謝謝維沙爾。它爲我工作。 –

+0

設置圖標有2個由xpath標識的節點,一個是可見的,另一個是不可見的,都存在於DOM中。你試圖點擊不可見元素,因爲它首先出現;因此,你得到「元素目前不可見,因此可能不會互動......」異常。因此,上面的代碼將採用由xpath標識的兩個元素並檢查其是否可見。如果可見,然後單擊。 –

0

問題出在你的xpath上。我試着用它的id工作。 Thread.sleep是個壞主意。不要與去改用WebDriverWait

總之下面的代碼片段會點擊設置齒輪,然後設置,等到面板被完全加載

而且通過這個Gmail Selenium,看看我是如何處理的設置沒有了Thread.sleep

Gmail登錄
public static void clickSetting(WebDriver driver) { 
    WebDriverWait wait = new WebDriverWait(driver, 60); 
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=':3d']"))).click(); 
    System.out.println("Clicked On Settings Gear"); 
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@role='menuitem']/div[.='Settings']"))).click(); 
    System.out.println("Clicked On Setting"); 
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class='dt']"))); 
    System.out.println("Settings Visible"); 
} 
+0

嗨馬丹, 感謝您分享代碼。 我使用了相同的代碼,您將它發送給gmail。但它不適合我。 錯誤消息: - 在等待由By.xpath定位的元素的可見性60秒後超時:// * [@ id =':3d'] Build info:version:'2.41。0',修訂版:'3192d8a',時間:'2014-03-27 17:18:15' 我正在嘗試的場景中,您需要打開收件箱的第一封電子郵件。然後嘗試點擊設置檔 - >設置。 –

+0

@Harikishen此代碼僅用於單擊設置我沒有做任何事情打開收件箱的第一封電子郵件 – Madhan

+0

只是爲了澄清。當收件箱的第一封電子郵件打開並且您嘗試點擊設置檔時,相同的密碼將無法使用。你會得到同樣的錯誤。這是我面臨的問題。 –

0

試試這個:

package yourPackageName; 

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; 

public class TestClass2 { 
static WebDriver driver; 
public static void main(String[] args) { 
    System.setProperty("webdriver.chrome.driver", "c:/chromedriver.exe"); 
    driver = new ChromeDriver(); 
    Step_1_LaunchApp(); 
    Step_2_LoginUsingCredentials(); 
    Step_3_ClickOnInbox(); 
    Step_4_ClickOnFirstEmail(); 
    Step_5_ClickOnComposeEmail(); 
    Step_6_ClickOnSettingsIcon(); 
} 

private static void Step_6_ClickOnSettingsIcon() { 
    try{ 
     driver.findElement(By.xpath("//*[@class='aos T-I-J3 J-J5-Ji']")).click(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

private static void Step_5_ClickOnComposeEmail() { 
    try{ 
     driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

private static void Step_4_ClickOnFirstEmail() { 
    try{ 
     driver.findElement(By.xpath("//div[@role='tabpanel'][1]//table//tr[1]")).click(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
    waitTimeInSecond(2); 
} 

private static void Step_3_ClickOnInbox() { 
    driver.findElement(By.xpath("//span/a[contains(text(),'Inbox')]")).click(); 
    waitTimeInSecond(2); 
} 

private static void Step_2_LoginUsingCredentials() { 
    driver.findElement(By.id("Email")).sendKeys("[email protected]"); 
    driver.findElement(By.id("next")).click(); 
    waitTimeInSecond(2); 
    driver.findElement(By.id("Passwd")).sendKeys("Password"); 
    driver.findElement(By.id("signIn")).click(); 
    waitTimeInSecond(5); 
} 

private static void Step_1_LaunchApp() { 
    driver.get("http://gmail.com"); 
} 

public static void waitTimeInSecond(int waitTime){ 
    try{Thread.sleep(waitTime*1000);}catch(Exception e){} 
    } 
} 

將您的憑證放在第一位並運行腳本,它會點擊設置檔。