Joseph Smith
Joseph Smith

Reputation: 1

How can I click a button with Selenium in a pop up window? Tried Various Methods with No success

I'm using Selenium with Java to automate a process I perform at work. It goes through a computer supplier site and submits a warranty request. I'm having a lot of trouble clicking the "Close" button in a popup box. The prompt doesn't always appear and it's generated with javascript. I actually wrote and had this working in Python but I'm a college student and my degree program uses java so I wanted to switch it over. I'm also using Google Chrome and the Chromedriver.

Any help is greatly appreciated, I would love some more things to try. My next step after everything below is an Action to move the mouse to the button but I haven't looked into that yet.

UPDATE: My checkIfExistsByXpath method was incorrect AND I needed a 2 sec Thread.Sleep(). The method should have been:

public boolean checkIfExistsByXpath(WebDriver driver, String xpath){
    try {
        driver.findElement(By.xpath(xpath));
    } catch (org.openqa.selenium.NoSuchElementException e){
        e.printStackTrace();
        return false;
    } return true;

Not what I have down below.

What I have tried:

The button does not have an ID. I attempted to get the value by xpath with a wait, this gives me a NoSuchElement exception and says it isn't the correct xpath I have triple checked the xpath and it's copied straight from the source, I get the same exception using the checkIfExistsByXpath method I made:

WebElement okButton = new WebDriverWait(driver, 30).until(
        ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"modalDuplicateDispatch\"]/div/div/div[3]/button")));
okButton.click();

I tried to put the thread to sleep (This worked in Python), I also tried wait(5000):

Thread.sleep(5000);
WebElement okButton = new WebDriverWait(driver, 30).until(
        ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"modalDuplicateDispatch\"]/div/div/div[3]/button")));
okButton.click();

I tried to get all the elements from the class and click them each in a loop, this gave me an elementClickIntercepted exception, I also tried this with the modal class:

    List<WebElement> buttonElements = driver.findElements(By.className("btn btn-primary"));
    for (WebElement buttonElement : buttonElements) {
        buttonElement.click();
    }

I tried to check if it was an alert box, it skipped right over this so I don't think its an alert box:

if (isAlertPresent)(driver){
        driver.switchTo().alert().accept();
}

if (isAlertPresent)(driver){
        driver.switchTo().alert().dismiss();
}

I tried to use the Javascript Executor, I don't remember the exact code off the top of my head but It didn't work either.

Functions to check if the pop up is present

public boolean isAlertPresent(WebDriver driver){
    try {
        driver.switchTo().alert();
        return true;
    }   // try
    catch (NoAlertPresentException Ex) {
        return false;
    }   // catch
}   // isAlertPresent()

public boolean checkIfExistsByXpath(WebDriver driver, String xpath){
    try {
        driver.findElement(By.xpath(xpath));
        return true;
    } catch (NoSuchElementException e){
        e.printStackTrace();
    } return false;

}

HTML Class From Webpage

<div class="modal" id="commonNotificationModal" style="display: none;">
        <div class="modal-dialog fadeDown in" role="dialog" aria-hidden="true">
            <div class="modal-content">
                <div class="modal-header">
                    <button data-dismiss="modal" aria-label="Close" class="close" type="button">
                        <clr-icon aria-hidden="true" shape="close"></clr-icon>
                    </button>
                    <h3 class="modal-title"></h3>
                </div>
                <div class="modal-body">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" data-dismiss="modal">OK</button>
                </div>
            </div>
        </div>
    </div>

Upvotes: 0

Views: 5362

Answers (5)

evan.zhu
evan.zhu

Reputation: 1

if popup instead of forcing you to click the close button, try clicking a blank space

actions.moveByOffset(200,400).click().perform();

Upvotes: 0

Ranvijay Singh
Ranvijay Singh

Reputation: 9

Try This method:

Driver.SwitchTo().frame("Fancybox-frame");
Driver.findElement("enter your xpath to close popup").click();
Driver.SwitchTo().defaultContent();

Upvotes: 0

Shelson Xavier
Shelson Xavier

Reputation: 11

driver.switch_to.alert.accept() or driver.switch_to.alert.dismiss()

Upvotes: 1

Justin Lambert
Justin Lambert

Reputation: 978

when popup appearing above parent window you should do window handles in selenium.

Upvotes: 0

RichEdwards
RichEdwards

Reputation: 3743

Try a simpler xpath - it's generic and will hopefully get a hit:

By.xpath("//button[text()='OK']")

I know you say you copied your one from python (where python worked) but it looks off. Selenium is telling you it's not found so it can't hurt to try something different. If you get lots of hits matching the xapth- try findelements to see if any other hits are the one you want - then look to make the identifier less generic from there.

I could be way off, but I've got a feeling it's the double quotes and the escape character around modalDuplicateDispatch. i.e. This bit: (By.xpath("//*[@id=\"modalDuplicateDispatch\"]/

(I also don't see modalDuplicateDispatch in your web page source so i'm assuming that's one level higher?)

I normally use single quote around the xpath text. I had a quick look around:

java identifier examples - Java using ' ' for xpath string

python identifier examples - Python using " " for xpath string

....Worth a try?

Upvotes: 1

Related Questions