无法使用java单击selenium webdriver中的弹出按钮

时间:2016-09-13 10:51:42

标签: javascript java selenium

我的Html

<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);



}
}

我无法点击&#34;确定&#34;按钮我得到的异常元素不可见。 然后我实现了Webdriver等待,但它也没有工作,我得到了timeoutexception。请帮我解决这个问题。我的代码工作正常,直到双星号线。

3 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:0)

我不明白您是否尝试过单击WebElement方法element.click(); 您可以指定WebDriver实现并发布日志。

我猜是点击该按钮会打开弹出窗口config.driver.findElement(By.xpath("//a[@data-ng-click='resetEmailCollectionTab()']")).click();

可以检查这个布尔值是否为真

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();

答案 2 :(得分:-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);