简体   繁体   English

无法使用Java在Selenium Webdriver中单击弹出按钮

[英]Unable to click on a popup button in selenium webdriver with java

My Html 我的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>

My Java 我的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);



}
}

I am not able to click on "OK" Button I a getting exception element not visible. 我无法单击“确定”按钮,但正在获取异常元素不可见。 Then I had implemented Webdriver wait but it also doesnt work and i got timeoutexception. 然后,我实现了Webdriver wait,但它也无法正常工作,并且我收到了timeoutexception。 Please help me regarding this.My code is working fine upto the double asteric mark line. 请对此提供帮助。我的代码在双星号行之前都可以正常工作。

When a popup happens, I believe it's registered as a new window within the driver. 当发生弹出窗口时,我相信它会在驱动程序中注册为新窗口。 You may need to switch to the new window in order to resolve the elements on that page. 您可能需要切换到新窗口才能解析该页面上的元素。

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

i do not understand if you tried the click on the WebElement method element.click(); 我不明白您是否尝试单击WebElement方法element.click(); ;。 Can you specify your WebDriver implementation and post the logs. 您可以指定WebDriver实现并发布日志吗?

I guess it's that click opens the popup, config.driver.findElement(By.xpath("//a[@data-ng-click='resetEmailCollectionTab()']")).click(); 我猜是单击打开了config.driver.findElement(By.xpath("//a[@data-ng-click='resetEmailCollectionTab()']")).click();

can you check if this boolean value is true 您可以检查此布尔值是否为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();

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM