如何使用Selenium Webdriver单击元素

时间:2019-06-13 20:21:05

标签: java selenium xpath css-selectors webdriverwait

我正在尝试使用Selenium Webdriver在UI上单击弹出警报消息。

问题是,即使我显式或隐式等待,它也不会单击接受或取消。除了点击弹出消息,还有其他选择吗?我试图通过Robot发送密钥并按Enter,但是它也无法正常工作。

单击“确定”弹出消息功能:

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

这是弹出窗口的html外观:

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

它必须在IE中,除IE外,我们不允许使用其他任何东西

更新:确认框功能


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

1 个答案:

答案 0 :(得分:0)

似乎该元素不是一个Alert而是一个<input>元素,并且在click()上必须诱导 WebDriverWait elementToBeClickable(),您可以使用以下任一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();