简体   繁体   English

如何使用Selenium Webdriver单击元素

[英]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. 我正在尝试使用Selenium Webdriver在UI上单击弹出警报消息。

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: 这是弹出窗口的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;">

It has to be in IE, we are not allow to use anything except IE 它必须在IE中,我们不允许使用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 : 看起来元素不是 Alert而是<input>元素,并且要在元素上click() ,您必须为elementToBeClickable()引入WebDriverWait ,并且您可以使用以下任一定位器策略

  • cssSelector : cssSelector

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[id$='_Content_ContentPlaceHolderMain_Continue'][value^='Continue'][name$='Continue']"))).click(); 
  • xpath : 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(); 

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

相关问题 如何使用 JavaScript 单击 Selenium WebDriver 中的元素? - How to click an element in Selenium WebDriver using JavaScript? 如何使用Safari的JavaScript双击Selenium Webdriver上的元素 - How to double click an element on Selenium Webdriver using JavaScript for Safari 如何使用 selenium webdriver 悬停并单击不可见的元素? - How to Hover over and click on an invisible element using selenium webdriver? 使用 Selenium Webdriver 单击 JQUERY 元素时出现问题 - Problems to click in a JQUERY element using Selenium Webdriver 无法使用 selenium webdriver 单击路径元素 - Not able to click an path element using selenium webdriver 使用Selenium Webdriver进行自动化,但无法单击元素 - Using selenium webdriver for automation but Not able to click on element 如何使用 selenium webdriver 单击角度元素? - How to click an angular element with selenium webdriver? 如何单击 Selenium WebDriver 中的隐藏元素? - How to click on hidden element in Selenium WebDriver? 如何在 selenium webdriver 中点击一个 svg 元素 - How to click an svg element in selenium webdriver 如何从列表中选择元素,单击它,返回列表,然后使用Selenium Webdriver选择下一个元素 - How to select element from the list, click on it, go back to the list and select next element using selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM