2014-09-29 73 views
4

我使用Selenium 2.x WebDriver進行java項目的自動化。 當自動化進行時,它會到達一個頁面,當點擊一個提交按鈕時會出現一個「彈出窗口」,並且自動化不能繼續進行。Selenium自動化不適用於彈出窗口

代碼

public void writeSite(WebDriver driver, ZoneTest zone) throws BiffException, InterruptedException, IOException 

    //Creates a new zone for testing 

    General.elementClick_xpath(driver, Locators.siteMenuDropBoxXpath); 
    General.waitToLoad(General.WAIT_MIN); 
    General.elementClick_xpath(driver, Locators.viewSitesButtonXpath); 
    General.elementClick_xpath(driver, Locators.viewDataPointDetailsXpath); 
    General.waitToLoad(General.WAIT_AVG); 

    General.elementClick_xpath(driver, Locators.addZoneXpath); 

    General.waitToLoad(General.WAIT_AVG); 

    General.inputTextFieldEnter_Id(driver, "name", zone.zoneName); 
    General.inputTextFieldEnter_Id(driver, "description",zone.zoneDescription); 
    General.inputTextFieldEnter_Id(driver, "urlExtension", zone.urlExtension); 
    General.inputTextFieldEnter_Id(driver, "timeSpentThreshold", zone.thresholdTime); 
    General.inputTextFieldEnter_Id(driver, "tuningNumber", zone.tuningNumber); 

    **General.elementClick_xpath(driver, Locators.createZoneSubmitXpath);** 

    //Here a new pop up window apppears. And the following codes 3 lines doesnt work. 

    General.inputTextFieldEnter_Id(driver, "active", zone.act); 
    General.inputTextFieldEnter_Id(driver, "userid", zone.uid); 
    General.elementClick_xpath(driver, Locators.SubmitXpath) 
} 


public class General 
{ 
    public static final long WAIT_MICRO = 500; 
    public static final long WAIT_MIN = 2000; 
    public static final long WAIT_AVG = 5000; 
    public static final long WAIT_MAX = 5500; 
    public static String baseUrl =""; 


    //Method to wait 
    public static void waitToLoad(long milliSeconds) throws InterruptedException { 
    Thread.sleep(milliSeconds); 
    } 

    // Method to load the Url taken from the config.property file 

    public static void loadBaseUrl(WebDriver driver){ 
    baseUrl = PropertyUtility.getProperty("Baseurl"); 
    driver.get(baseUrl); 
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
    } 

    public static void inputTextFieldEnter_xpath(WebDriver driver, String LocatorId, String ValueToBeEntered) throws InterruptedException {  
    System.out.println("Log in user name_inputTextField_outside if: "+ ValueToBeEntered); 
    WebElement inputTextField = driver.findElement(By.xpath(LocatorId)); 
    General.waitUntilElementVisible(driver, LocatorId); 
    inputFieldClear_xpath(driver,LocatorId); 
    inputTextField.sendKeys(ValueToBeEntered);  
} 

如何切換視圖到新的彈出窗口?

+0

不知道爲什麼4個upvotes這個簡單回答的問題! – 2014-09-29 12:05:09

+0

[如何處理在使用Java的硒webdriver彈出]可能重複(http://stackoverflow.com/questions/19403949/how-to-handle-pop-up-in-selenium-webdriver-using-java) – 2014-09-29 12:05:27

+2

@Vignesh Paramasivam和3 upvotes和2收益的問題%)似乎這些傢伙是朋友:D – 2014-09-30 10:53:14

回答

8

如果你想在彈出窗口中進行任何操作,這裏是WebDriver邏輯來選擇彈出窗口。

driver.switchTo().window("<window name>"); 
1

如果有任何彈出窗口出現的時候,那麼你就可以切換到該window.Because除非你提到的窗口名不會自動識別新的窗口。

語法:set ws = driver.get windowshandles();

Iterator itr=ws.iterate(); 

    String W1=(String)itr.next(); 

    String W2=(String)itr.next(); 

driver.switchTo()。window(W1); (或)

(或)

driver.switchTo()。window(W2);

(或) ......

0

您需要先切換該窗口越看越你想要什麼做了手術。你必須使用下面的命令來切換窗口。

driver.switchTo().window("Window ID"); 

您可以使用下面的命令獲取所有打開的窗口唯一ID。

Set<String> windows = driver.getWindowHandles(); 

在您的情況下,請使用下面的代碼切換窗口。有關如何處理窗口和彈出

String vBaseWindowHandle = driver.getWindowHandle(); 
    Set<String> windows = driver.getWindowHandles(); 

    for(String temp : windows) 
    { 
     driver.switchTo().window(temp); 
    } 

    // Do code for new window 

    driver.close();  
    driver.switchTo().window(vBaseWindowHandle); 

富勒更多信息,請訪問以下網址 https://trickyautomationworld.blogspot.in/2018/03/how-to-handle-windows-in-selenium.html https://trickyautomationworld.blogspot.in/2018/03/how-to-handle-popup-in-selenium.html