如何使用 selenium webdriver 遍历并单击“下一步”和“取消”按钮(在弹出窗口上)

How to traverse and click on Next & Cancel Button (On PopUp) using selenium webdriver

我在网页上有一个弹出窗口 window,我需要在其中单击 "Next" 和 "Cancel" 按钮。我已尝试导航并找到 CSS 选择器,但无法找到并单击“下一步并取消”按钮。 以下是我将鼠标悬停在“下一步和取消”按钮上后出现的 Fire bug。

<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
    <div class="ui-dialog-buttonset">
        <button class="ui-button ui-widget ui-state-default ui-corner-all
         ui-button-text-only" type="button" role="button" aria- 
         disabled="false">
             <span class="ui-button-text">Next</span>
        </button>
        <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" role="button" aria-disabled="false">
            <span class="ui-button-text">Cancel</span>
        </button>
     </div>
</div>

您可以通过 xpath 和按钮文本

进行搜索
// click on next button
driver.findElement(By.xpath("//span[contains(text(), 'Next')]")).click();

// click on cancel button    
driver.findElement(By.xpath("//span[contains(text(), 'Cancel')]")).click();

或使用cssSelector

driver.findElement(By.cssSelector(".ui-button-text:contains('Next')")).click();
driver.findElement(By.cssSelector(".ui-button-text:contains('Cancel')")).click();

基本上如果是弹窗,就分为alert、confirm和prompt三种。我将为您提供警报的解决方案,该解决方案也应与确认一起使用并尝试一下。如果它有效,请告诉我,如果我有更多信息,我可以帮助你。我正在使用 C#。使用 IWebDriver 接口的“SwitchTo() 方法和 IAlert 接口的 Alert() 方法(这有 Accept 和 Dismiss 方法,它们是您在弹出窗口中的 OK/Cancel)。

代码:(假设触发弹出窗口的操作已经执行)

 IAlert alert = Driver.SwitchTo().Alert();   
  alert.Accept();
 //or alert.Dismiss();

如果点击跨度(根据@Guy 的回答)不起作用,请尝试 -

// click on next button
driver.findElement(By.xpath("//button[contains(., 'Next')]")).click();

// click on cancel button    
driver.findElement(By.xpath("//button[contains(., 'Cancel')]")).click();