Selenium Webdriver: The method alert() is undefined for the type WebDriver.TargetLocator

你离开我真会死。 提交于 2020-01-15 11:59:06

问题


I am trying to handle alerts using Selenium Webdriver, according the selenium documentation the correct implementation is:

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

However I am getting error message The method alert() is undefined for the type WebDriver.TargetLocator

what version of Selenium 2 has this web driver api


回答1:


I'm using selenium 2.3 and it works for me. From what I've seen, alerts only work with windows that ONLY have the ok button. Thats where you put the alert.accept(); statement in. Another problem that I've seen with alerts is that it may jump over the alert so you may have to have a wait statement for that specific alert. Hope this helps.




回答2:


Alerts are not be supported by HmltUnit driver. In HtmlUnitDriver.java (Selenium 2.0.6) :

public Alert alert() {
  throw new UnsupportedOperationException("alert()");
}

Which driver are you using?




回答3:


You may need to use a separate import statement for Alert:

import org.openqa.selenium.Alert;

I had the same issue.



来源:https://stackoverflow.com/questions/7178029/selenium-webdriver-the-method-alert-is-undefined-for-the-type-webdriver-targe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!