简体   繁体   中英

How to click on the element using Selenium Webdriver

I'm trying to click on a pop-up alert message on a UI with Selenium Webdriver.

The problem is, it is not clicking on accept or cancel even if I explicitly and implicitly wait. Is there any other alternative to clicking on a pop-up message. I tried to send key by Robot and press enter, but it did not work too.

click ok popup message function:

    try {
            WebDriverWait wait = new WebDriverWait(driver, 40);
            wait.until(ExpectedConditions.alertIsPresent());
            Alert alert = driver.switchTo().alert();
            report.log(LogStatus.INFO, "Displayed Pop-up Window Alert Message ->  " + alert.getText() + " for the Field -> " + fieldName);
            System.out.println("Displayed Pop-up Window Alert Message ->  " + alert.getText() + " for the Field -> " + fieldName);
            alert.accept();
            Thread.sleep(3000);
        } catch (NoAlertPresentException ex) {
            System.err.println("Error came while waiting for the alert popup. ");
            report.log(LogStatus.INFO, "Alert pop up box is NOT populating when user clicks on: ");
        }

this is what the html looks like for the popup:

<input type="submit" name="ctl00$ctl00$Content$ContentPlaceHolderMain$Continue" value="Continue..." 
            onclick="if(warnOnDelete('ctl00_ctl00_Content_ContentPlaceHolderMain_EditRadioOptions_1',"
                    + "'Please confirm if you wish to delete.') == false) return false;" 
                    id="ctl00_ctl00_Content_ContentPlaceHolderMain_Continue" style="width:100px;">

It has to be in IE, we are not allow to use anything except IE

Update: function for the confirm boxes


        function warnOnDelete(deleteButtonID, msg) {
            var deleteRadioButton = document.getElementById(deleteButtonID);
            if (deleteRadioButton != null) {
                if (deleteRadioButton.checked == true)
                    return confirm(msg);
            }
            return true;
        }

Seems the element is not an Alert but an <input> element and to click() on the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies :

  • cssSelector :

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[id$='_Content_ContentPlaceHolderMain_Continue'][value^='Continue'][name$='Continue']"))).click(); 
  • xpath :

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(@id, '_Content_ContentPlaceHolderMain_Continue') and starts-with(@value, 'Continue')][contains(@name, 'Continue')]"))).click(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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