2016-08-02 24 views
2

I am not able to fill this form and also not able to handle the alert obtain on top left corner如何處理彈出式網頁並同時提醒?

請幫我填寫此表格,然後進入網站並處理警報。

這裏是我的代碼是不工作:

public class FirstCry { 

public static void main(String[] args) throws InterruptedException { 


      System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\CP-SAT\\Chromedriver\\chromedriver.exe"); 
      WebDriver a = new ChromeDriver(); 
      a.get("http://www.firstcry.com/"); 
      Thread.sleep(5000L); 
      a.manage().window().maximize(); 

      String k = a.getPageSource(); 
      System.out.println(k); 

      WebDriverWait Wait = new WebDriverWait(a, 30); 
      Wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='amt']/div[2]/div[1]/div[1]/div[3]/div"))); 

      WebElement b = a.findElement(By.xpath(".//*[@id='amt']/div[2]/div[1]/div[1]/div[3]/div")); 
      b.click(); 

     } 
} 
+0

你爲什麼不使用窗口句柄。 –

+0

所有在單個窗口中打開。因此,沒有使用Windows處理程序 –

+0

@KishanPatel嘗試更新的答案,它會處理您的彈出窗口和通知警報兩個..感謝:) –

回答

1

在你website彈出存在的iframe內部ID爲iframe_Login,你需要切換這iframe發現如下彈出窗口的關閉按鈕之前: -

//Create this prefs to handle notification popup 
Map<String, Object> prefs = new HashMap<String, Object>(); 
prefs.put("profile.default_content_setting_values.notifications", 2); 

//Initialize chrome option to add prefs 
ChromeOptions options = new ChromeOptions(); 
options.setExperimentalOption("prefs", prefs); 

//Now initialize chrome driver with chrome option to handle notification alert 
WebDriver a = new ChromeDriver(options) 

a.get("http://www.firstcry.com/"); 
a.manage().window().maximize(); 

WebDriverWait wait = new WebDriverWait(a, 30) 

//Now find iframe and switch to it 
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe_Login")); 

//Now find the popup close button 
WebElement b = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[class = '_pop_close _pop_reg_bg']"))); 
b.click(); 

//Now switch back from frame to default content for further steps 
a.switchTo().defaultContent(); 

//Now do your further stuff 

希望它能幫助.. :)

+0

優秀..那麼彈出窗口呢。我用alert.dismiss(); ,但它不起作用。你能幫助我彈出左上角的那個彈出窗口嗎? –

+0

我可以關閉瀏覽器中的一些設置,以便在我運行腳本時不會再次出現? –

+0

@KishanPatel明白了......不需要一些額外的設置..只是使用鉻pref你可以處理它..嘗試更新的答案.. :) –