WebDriver closing the popup

前端 未结 1 663
逝去的感伤
逝去的感伤 2021-01-25 01:56

I have web-driver test which is stuck because a pop-up window appears. How ca I close it in test?

Here is my code:

@Test
 public void canGoToSomePage() t         


        
相关标签:
1条回答
  • 2021-01-25 02:32

    Try this,

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

    I have never used alert before, I used to silent the pop up using JS before. You could do that too, but i guess Alert would be the first choice.

    EDIT#1

    Here is how to use Java script to silent the pop up. Note that it has to be executed BEFORE the click that causes the popup to show up. Based on whether your pop up is alert, confirm or prompt, you will have to use something like below.

    ((JavascriptExecutor)driver).executeScript("window.alert = function(msg) { return true; }");
    ((JavascriptExecutor)driver).executeScript("window.confirm = function(msg) { return true; }");
    ((JavascriptExecutor)driver).executeScript("window.prompt = function(msg) { return true; }");
    
    0 讨论(0)
提交回复
热议问题