2013-05-17 83 views
-1

我有一個應用程序,我嘗試單擊一個按鈕,並作爲回報,它將彈出一個窗口以填充新的用戶表單。這不像一個彈出窗口,因爲它有一些輸入字段以及「保存」和「取消」按鈕。它看起來類似於Facebook中的彈出式窗口。無法切換到彈出窗口,並使用Java在Webdriver中彈出窗口中找到任何元素

下面的代碼我試圖與

Set beforePopup = driver.getWindowHandles(); 
    driver.findElement(By.xpath("/html/body/div/div/div/div[3]/div/div[2]/div[2]/table/tbody/tr/td[3]/table/tbody/tr/td[2]/em/button")).click(); 
    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS); 
    Set afterPopup = driver.getWindowHandles(); 
    afterPopup.removeAll(beforePopup); 
    if(afterPopup.size() == 1) { 
      driver.switchTo().window((String)afterPopup.toArray()[0]); 
     } 

    //driver.switchTo().window("Add New User"); 
    //selenium.type("userDetailsBean.firstName","alan1"); 
    //WebElement btnStartQueryInside = driver.findElement(By.xpath("//html/body/div[14]/div/div/div/div/span")); 
    //btnStartQueryInside.getText(); 
    System.out.println(driver.getTitle()); 
    WebElement firstName = driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div/div/form/div/div/input")); 
    firstName.sendKeys("alan1"); 



    //driver.switchTo().alert(); 
    //driver.findElement(By.xpath("//html/body/div[14]/div/div/div/div")).getText(); 
    //WebElement firstName=driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div/div/form/div/div/input")); 
    //firstName.sendKeys("alan1"); 
    /*WebElement lastName=driver.findElement(By.id("userDetailsBean.lastName")); 
    lastName.sendKeys("harper"); 
    WebElement emailadd=driver.findElement(By.id("userDetailsBean.userEmail")); 
    emailadd.sendKeys("[email protected]"); 
    WebElement username=driver.findElement(By.id("userDetailsBean.userName")); 
    username.sendKeys("xalan1"); 
    WebElement password=driver.findElement(By.id("adminPassword")); 
    password.sendKeys("Derik123"); 
    WebElement repassword=driver.findElement(By.id("adminPassword2")); 
    repassword.sendKeys("Derik123"); 
    driver.findElement(By.xpath("//html/body/div[2]/div/div/div/div/div[2]/div/div/table/tbody/tr/td/table/tbody/tr/td[2]/em/button")).click(); 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);*/ 

請注意,在代碼中我評論一些輸入字段填充,因爲我覺得首先我將使它的工作只是1場。請幫助我如何繼續。

問題出在點擊彈出按鈕後,我不確定控件是否切換到彈出窗口。彈出後的gettitle會給出主窗口標題。並且無法使用xpath或id找到第一個輸入字段。

+1

那麼問題是什麼? – dratewka

+0

我編輯了添加問題的問題。 – mac

+0

您不需要切換到圖像中彈出的消息彈出窗口。直接與它交互。 – Amey

回答

2

Selenium中的術語window意味着實際的瀏覽器窗口,而不是幀或只是div。因此,你的邏輯錯誤,getWindowHandles()driver.switchTo().window("Add New User")不是你所追求的。

您需要的是driver.switchTo().frame()

driver.switchTo().defaultContent(); // optional, use only if driver is already in an iframe 

WebElement editUserForm = driver.findElement(By.cssSelector("iframe[src*='editUserForm']")); 
// there are other overloads (by frame name, index, id) and locators can be used here. 
driver.switchTo().frame(editUserForm); 

// make sure your locator here is correct 
WebElement lastName = driver.findElement(By.id("userDetailsBean.lastName")); // I doubt this is correct 
// from your screenshot, I'd suggest By.cssSelector("[id*='userDetailsBean.lastName'] input") 
lastName.sendKeys("harper"); 

也只是一個親切的頭,你的代碼聞起來。

  • 你implicitlyWait等待的使用似乎是錯誤的,請閱讀the documentationthe post

  • 你從哪裏得到selenium.type("userDetailsBean.firstName","alan1");?你是否只是從其他地方複製該行?這看起來像Selenium RC給我的。

  • 爲什麼driver.switchTo().alert()?你讀過the documentation這是什麼?
  • 請不要使用絕對xpath。
  • 嘗試使用頁面對象,如果你沒有在你的真實項目中做到這一點。 (這很好,如果你只是想說明這裏的問題)
+0

它爲我工作now.thanks ..我開始工作的webdriver編碼2天后。感謝您的所有提示。它應該幫助我。 – mac

相關問題