2016-09-01 6 views
0

내 코드가 더 이상 실행되지 않는 팝업 창을 닫으려고합니다. 그러나 나는 그것을 할 수 없다. 닫히지 않는 'Free Pound 5 Voucher'라는 팝업창이 있습니다.Selenium에서 팝업 창을 닫는 방법?

아래 코드는 제 코드입니다. 확인하고 제안하십시오.

public class Alldetails { 
    WebDriver driver; 
    public List<String> links1 = new ArrayList<String>(); 

    @BeforeTest 
    public void beforeTest() { 
     System.setProperty("webdriver.chrome.driver", "C:\\Pankaj\\chromedriver.exe"); 
     driver = new ChromeDriver(); 
     // driver = new FirefoxDriver(); 
     driver.manage().window().maximize(); 
    } 

    @Test 
    public void f() { 
     driver.get("http://www.debenhams.com/kids/t-shirts-tops/boys"); 
     driver.findElement(
       By.xpath(".//*[@id='dijit__WidgetsInTemplateMixin_0']/div/div[1]/button")) 
       .click(); 

     WebElement a = driver.findElement(By.className("products_count")); 
     String product_count = a.getText(); 
     Integer x = Integer.valueOf(product_count); 
     System.out.println(x); 
     for (int i = 1; i < (x/60) + 1; i++) { 
      driver.get("http://www.debenhams.com/kids/t-shirts-tops/boys" + "?pn=" + i); 

      List<WebElement> links = driver 
        .findElements(By.className("item_container").tagName("a")); 
      for (WebElement abcx : links) { 
       String ax = abcx.getAttribute("href"); 
       // if(!links1.contains(ax)) 
       links1.add(ax); 
      } 

      for (String b : links1) { 
       if (b.contains("/webapp/wcs/stores/servlet/prod")) 
        System.out.println(b); 
      } 
     } 
    } 

    @AfterTest 
    public void afterTest() { 
    } 
} 
+0

팝업이 표시되는 코드에서이 코드는 무엇입니까? 아니면 그 팝업이 나타나면? 제발 좀 더 알려 주실 래요? –

+0

아래 코드 줄 뒤에 팝업이 나타납니다. 'driver.get ("http://www.debenhams.com/kids/t-shirts-tops/boys"+ "pn ="+ i); ' –

+0

어떤 종류의 팝업? 자바 스크립트 경고? HTML/CSS 팝업? 브라우저 팝업? –

답변

0

당신은 같은 것을 할 필요가 :

Alert alert = driver.switchTo().alert(); 
// Prints text and closes alert 
System.out.println(alert.getText()); 
alert.accept(); 

또는 필요에 따라

alert.dismiss(); 

.

경고가 표시되기 전에 코드가 실행되는 경우가 AlertNotPresentException을받을 수

편집 할 수 있습니다. 이 같은 지연을 추가 할 수 있다는 것을 피하기 위해 :

WebDriverWait wait = new WebDriverWait(driver, 15, 100) 
wait.until(ExpectedConditions.alertIsPresent()) 

이는 것 셀레늄은 경고가 나타나는 경우, 100 밀리 세컨드마다 검사를 15 초 기다립니다. 이 스 니펫은 내 것이 아니며 여기에서 가져 왔습니다. Selenium how to get the alert Alert message

+0

오류 메시지가 나타나지 않지만 그 후에 인쇄해야하는 URL이 없습니다. 이 페이지의 모든 제품 URL을 가져 오기 위해이 코드를 작성했습니다. –

+0

무엇이 예외를 던지고 있습니까? org.openqa.selenium.NoAlertPresentException : 열려있는 경고 없음 –

+0

내 대답을 편집했습니다 – Aurasphere

관련 문제