在 Selenium WebDriver (Selenium 2) 中处理警报

发布于 2024-10-19 21:17:06 字数 383 浏览 4 评论 0原文

driver.findElement(By.xpath("//input[@value='添加']")).click(); 
//Pops out an Alert and program stops, does not continue 

如何点击提醒?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

啊啊啊啊  怎么没有人呢? (TRANS: ahahahahaha why there is no one here to reply my post?)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我顶  (TRANS: Let me promote this post!)
driver.findElement(By.xpath("//input[@value='添加']")).click(); 
//Pops out an Alert and program stops, does not continue 

how to click the alert?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

啊啊啊啊  怎么没有人呢? (TRANS: ahahahahaha why there is no one here to reply my post?)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我顶  (TRANS: Let me promote this post!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

允世 2024-10-26 21:17:07
Alert alert = driver.switchTo().alert();
alert.accept();

如果您想取消弹出窗口,请使用以下命令:

 alert.dismiss();

而不是

 alert.accept():
Alert alert = driver.switchTo().alert();
alert.accept();

If you want to cancel the pop up use the following:

 alert.dismiss();

instead of

 alert.accept():
╰つ倒转 2024-10-26 21:17:06

从最新的 selenium 2 版本开始,可以做到这一点(至少使用 FirefoxDriver):

    driver.switchTo().alert().accept();

As of the latest selenium 2 release, this can be done (at least using the FirefoxDriver):

    driver.switchTo().alert().accept();
一杆小烟枪 2024-10-26 21:17:06

在 Selenium 2 中,当前警报仅在 Firefox 浏览器中处理。您无需指定测试所使用的语言,但以下是如何使用 ruby​​ 处理警报。 (这取自 selenium wiki 上的 Ruby Bindings 页面)。

Javascript 警报/确认

您可以使用 webdriver 来处理 javascript 警报和确认对话框。两者的实现是相同的。

注意:目前该 API 仅在 Firefox 中可用(或在使用远程服务器的 Firefox 中),并且只能捕获加载后生成的警报/确认。

require "selenium-webdriver"

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://mysite.com/page_with_alert.html"

driver.find_element(:name, 'element_with_alert_javascript').click
a = driver.switch_to.alert
if a.text == 'A value you are looking for'
  a.dismiss
else
  a.accept
end

In Selenium 2, currently alerts are only handled in the Firefox browser. You don't specify what language you're using for your tests, but here's how to handle an alert using ruby. (This is taken from the Ruby Bindings page on the selenium wiki).

Javascript Alert/Confirm

You can use webdriver to handle javascript alert and confirm dialogs. The implementation for both is the same.

Note: At this time the API is only available in Firefox (or in Firefox using the remote server), and only alert/confirms that are generated post load can be captured.

require "selenium-webdriver"

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://mysite.com/page_with_alert.html"

driver.find_element(:name, 'element_with_alert_javascript').click
a = driver.switch_to.alert
if a.text == 'A value you are looking for'
  a.dismiss
else
  a.accept
end
辞慾 2024-10-26 21:17:06

在 Selenium 2 的早期版本中,我没有选择通过覆盖 Javascript 中的 window.alert 来处理 Internet Explorer 中的警报:

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
// Override window.alert to store the prompt and accept it automatically
js.ExecuteScript("window.alert = function(msg) { document.bAlert = true; document.lastAlert=msg; }");

// Do some stuff...

// Check for alert
Object o = js.ExecuteScript("return document.bAlert");
if (o != null && (bool)o == true)
{
    //retrieve the alert message
    o = js.ExecuteScript("return document.lastAlert");
    // Do something with the alert text
}

Selenium 2.0b3 支持在 IE 和 Firefox 中处理警报,因此您可以执行以下操作:

IAlert alert = driver.SwitchTo().Alert();
// Get the text from the alert
string alertText = alert.Text;
// Accept the alert
alert.Accept();

但是,我无法使上述内容与是/否警报一起使用(Dismiss() 适用于“否”,但 Accept() 不适用于“是”)。我正在查看 IEDriver 以找出原因。

In previous version of Selenium 2 I have had no choice that to handle alerts in Internet Explorer by overriding the window.alert in Javascript:

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
// Override window.alert to store the prompt and accept it automatically
js.ExecuteScript("window.alert = function(msg) { document.bAlert = true; document.lastAlert=msg; }");

// Do some stuff...

// Check for alert
Object o = js.ExecuteScript("return document.bAlert");
if (o != null && (bool)o == true)
{
    //retrieve the alert message
    o = js.ExecuteScript("return document.lastAlert");
    // Do something with the alert text
}

Selenium 2.0b3 has support for handling Alerts in IE and Firefox, so you can do the following:

IAlert alert = driver.SwitchTo().Alert();
// Get the text from the alert
string alertText = alert.Text;
// Accept the alert
alert.Accept();

However, I have not been able to get the above to work with Yes/No alerts (Dismiss() works for No but Accept() doesn't work for Yes). I'm in the process of looking at the IEDriver to work out why this is.

‘画卷フ 2024-10-26 21:17:06
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://skynet:8081/1.htm");

var selenium = new WebDriverBackedSelenium(driver, driver.Url);
selenium.Start();

selenium.Click("css=input[type=button]");

Assert.AreEqual(selenium.GetConfirmation(), "Are you sure?");
Assert.AreEqual("OK", selenium.GetAlert());

// <input type="button" onclick="if(confirm('Are you sure?')) alert('OK'); else alert('Cancel');" value="Alert test" />

driver.Quit();
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://skynet:8081/1.htm");

var selenium = new WebDriverBackedSelenium(driver, driver.Url);
selenium.Start();

selenium.Click("css=input[type=button]");

Assert.AreEqual(selenium.GetConfirmation(), "Are you sure?");
Assert.AreEqual("OK", selenium.GetAlert());

// <input type="button" onclick="if(confirm('Are you sure?')) alert('OK'); else alert('Cancel');" value="Alert test" />

driver.Quit();
回梦 2024-10-26 21:17:06

您必须处理异常并运行警报处理程序代码,对于 Java:

      try{
         driver.findElement(By.xpath("//input[@value='添加']")).click(); 
      } catch(org.openqa.selenium.UnhandledAlertException e){
         Alert alert = driver.switchTo().alert();
         alert.accept();
         // you logic here what you want to do next
      }  

捕获此异常,然后您可以相应地接受或拒绝警报。

You will have to handle exception and run your handler code for Alert, for Java:

      try{
         driver.findElement(By.xpath("//input[@value='添加']")).click(); 
      } catch(org.openqa.selenium.UnhandledAlertException e){
         Alert alert = driver.switchTo().alert();
         alert.accept();
         // you logic here what you want to do next
      }  

Catch this exception, and then you can accordingly accept or reject alert.

难理解 2024-10-26 21:17:06

C# 代码:

IAlert alert = driver.SwitchTo().Alert();
alert.Accept(); 
System.Threading.Thread.Sleep(milliseconds);

C# code:

IAlert alert = driver.SwitchTo().Alert();
alert.Accept(); 
System.Threading.Thread.Sleep(milliseconds);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文