2016-09-13 123 views
0

我的HTML無法點擊一個彈出按鈕,硒的webdriver與Java

<div id="981bdff3-90a1-4966-ada9-6550b5a963bc" class="modal bootstrap-dialog type-primary fade size-normal in" aria-hidden="false" role="dialog" aria- labelledby="981bdff3-90a1-4966-ada9-6550b5a963bc_title" tabindex="-1" style="display: block; padding-right: 17px;"> 
<div class="modal-backdrop fade in" style="height: 351px;"></div> 
<div class="modal-dialog"> 
<div class="modal-content"> 
<div class="modal-header"> 
<div class="bootstrap-dialog-header"> 
<div class="bootstrap-dialog-close-button" style="display: none;"> 
<button class="close">×</button> 
</div> 
<div id="981bdff3-90a1-4966-ada9-6550b5a963bc_title" class="bootstrap-dialog-title">Are you sure?</div> 
</div> 
</div> 
<div class="modal-body"> 
<div class="bootstrap-dialog-body"> 
<div class="bootstrap-dialog-message">You will lose all data !</div> 
</div> 
</div> 
<div class="modal-footer" style="display: block;"> 
<div class="bootstrap-dialog-footer"> 
<div class="bootstrap-dialog-footer-buttons"> 
<button id="1b0400a9-c69b-429f-9bdd-11112b7cb3a4" class="btn btn-default">Cancel</button> 
<button id="3e4fec25-9538-4351-92b1-c7f9f8ce9574" class="btn btn-primary">OK</button> 
</div> 
</div> 
</div> 
</div> 
</div> 

我的Java

package Modules; 

import java.util.Iterator; 
import java.util.Set; 
import org.openqa.selenium.Alert; 
import org.openqa.selenium.By; 
import org.openqa.selenium.JavascriptExecutor; 
import org.openqa.selenium.Keys; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 
import org.testng.annotations.Parameters; 
import org.testng.annotations.Test; 
import Config.config; 

public class AddCollectionPoint_Email { 

@Test 
@Parameters({"DATAPROVIDER"}) 
public void Addcollection(String DataProvider) 
{ 

    config.driver.findElement(By.id("collectionPointsMenu")).click(); 
    config.driver.findElement(By.xpath("//a[@href='addCollectionPoint']")).click(); 
    config.driver.findElement(By.xpath("//a[@data-ng-click='resetEmailCollectionTab()']")).click(); 
    config.driver.findElement(By.xpath("//a[@data-ng-click='resetFtpCollectionTab()']")).click(); 
    **config.driver.findElement(By.xpath("//a[@data-ng-click='resetEmailCollectionTab()']")).click();** 
    WebElement element = config.driver.findElement(By.xpath("//div[@class='modal-content']")).findElement(By.xpath("//div[@class='modal-footer']")) 
      .findElement(By.xpath("//div[@class='bootstrap-dialog-footer-buttons']")).findElement(By.xpath("//button[@class='btn btn-primary']")); 

    JavascriptExecutor executor = (JavascriptExecutor)config.driver; 
    executor.executeScript("arguments[0].click();", element); 
    config.driver.findElement(By.xpath("//input[@name='selectedDataProvider']")).sendKeys(DataProvider); 



} 
} 

我不能夠「確定」按鈕,單擊我一個越來越例外元素不可見。 然後我實現了Webdriver等待,但它也不起作用,我得到了時間錯誤。請幫我解決這個問題。我的代碼工作良好,直到雙星標記線。

回答

-1

試試這個:

WebDriverWait wait = new WebDriverWait(config.driver, 20);   
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='modal-content']")))); 
    WebElement element = config.driver.findElement(By.xpath("//div[@class='modal-content']")).findElement(By.xpath("//div[@class='modal-footer']")) .findElement(By.xpath("//div[@class='bootstrap-dialog-footer-buttons']")).findElement(By.xpath("//button[@class='btn btn-primary']")); 
    JavascriptExecutor executor = (JavascriptExecutor)config.driver; 
    executor.executeScript("arguments[0].click();", element); 
    config.driver.findElement(By.xpath("//input[@name='selectedDataProvider']")).sendKeys(DataProvider); 
+0

他提到在WebDriverWait沒有幫助的說明,並導致_TimeoutException_ – Jeremiah

0

當彈出發生,我相信這是註冊爲驅動程序中的新窗口。您可能需要切換到新窗口才能解析該頁面上的元素。

public class AddCollectionPoint_Email { 

    @Test 
    @Parameters({"DATAPROVIDER"}) 
    public void Addcollection(String DataProvider) 
    { 

     config.driver.findElement(By.id("collectionPointsMenu")).click(); 
     config.driver.findElement(By.xpath("//a[@href='addCollectionPoint']")).click(); 
     config.driver.findElement(By.xpath("//a[@data-ng-click='resetEmailCollectionTab()']")).click(); 
     config.driver.findElement(By.xpath("//a[@data-ng-click='resetFtpCollectionTab()']")).click(); 


     //Capture the current Working Window handle 
     String currentWindowHandle = config.driver.getWindowHandle(); 

     //Performing this interaction causes the confirmation dialog to open. 
     config.driver.findElement(By.xpath("//a[@data-ng-click='resetEmailCollectionTab()']")).click();** 

     //At this point I expect there to be 2 window handles. My original and the new one opened by the previous invocation. 
     List<String> allHandles = config.driver.getWindowHandles(); 

     //assert size maybe? 

     allHandles.remove(currentWindowHandle); 

     String dialogHandle = allHandles.get(0); 
     try { 
      //Transfer focus to the dialog! 
      config.driver.switchTo().window(dialogHandle); 
      //Now we're on the dialog, find the button and click it. 
      WebElement element = config.driver.findElement(By.xpath("//div[@class='modal-content']")).findElement(By.xpath("//div[@class='modal-footer']")) 
       .findElement(By.xpath("//div[@class='bootstrap-dialog-footer-buttons']")).findElement(By.xpath("//button[@class='btn btn-primary']")); 
     } finally { 
      //All actions on the dialog are completed, switch back to the main window to continue process. 
      config.driver.switchTo().window(currentWindowHandle); 
     } 

     //I'm assuming this happens in the original window. Move into the try block if it happens in the dialog. 
     JavascriptExecutor executor = (JavascriptExecutor)config.driver; 
     executor.executeScript("arguments[0].click();", element); 
     config.driver.findElement(By.xpath("//input[@name='selectedDataProvider']")).sendKeys(DataProvider); 
    } 
} 
0

如果你試圖在WebElement方法點擊element.click(); 你可以指定你的webdriver實現和發佈日誌,我不明白。

我想那就是點擊打開彈出,config.driver.findElement(By.xpath("//a[@data-ng-click='resetEmailCollectionTab()']")).click();

,你可以檢查這個布爾值爲true

boolean isDisplayed = config.driver.findElement(By.xpath("//div[@class='modal-content']")).findElement(By.xpath("//div[@class='modal-footer']")) 
     .findElement(By.xpath("//div[@class='bootstrap-dialog-footer-buttons']")).findElement(By.xpath("//button[@class='btn btn-primary']")).isDisplayed();