7
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!)
4

7 に答える 7

12

最新のselenium2リリースの時点で、これは(少なくともFirefoxDriverを使用して)実行できます。

    driver.switchTo().alert().accept();
于 2011-03-08T20:57:00.780 に答える
4

以前のバージョンのSelenium2では、Javascriptでwindow.alertをオーバーライドすることにより、InternetExplorerでアラートを処理する選択肢がありませんでした。

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();

ただし、上記をYes / Noアラートで機能させることができませんでした(Dismiss()はNoで機能しますが、Accept()はYesでは機能しません)。私はこれがなぜであるかを解明するためにIEDriverを調べているところです。

于 2011-04-01T12:57:45.127 に答える
4

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
于 2011-03-03T06:25:04.963 に答える
2
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();
于 2011-08-12T23:32:18.040 に答える
2

例外を処理し、Alert 用のハンドラー コードを 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
      }  

この例外をキャッチすると、それに応じてアラートを受け入れるか拒否することができます。

于 2012-08-09T14:13:44.287 に答える
1

C#コード:

IAlert alert = driver.SwitchTo().Alert();
alert.Accept(); 
System.Threading.Thread.Sleep(milliseconds);
于 2012-02-13T23:42:16.810 に答える