【问题标题】:Selenium automation not working for a pop up windowSelenium 自动化不适用于弹出窗口
【发布时间】:2014-09-29 11:29:32
【问题描述】:

我正在使用 Selenium 2.x WebDriver 来自动化 java 项目。 当自动化继续进行时,它会到达一个页面,在该页面中,当单击提交按钮时,会出现“弹出窗口”并且无法继续自动化。

代码

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);     
}

如何将视图切换到新的弹出窗口?

【问题讨论】:

标签: java selenium-webdriver


【解决方案1】:

如果你想在弹窗中做任何操作,这里是WebDriver逻辑来选择弹窗。

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

【讨论】:

    【解决方案2】:

    如果出现任何弹出窗口,则必须切换到该窗口。因为除非您提及该窗口名称,否则它不会自动识别新窗口。

    语法: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);

    (或) ......

    【讨论】:

      【解决方案3】:

      您需要先切换该窗口,然后再进行任何您想要的操作。您必须使用以下命令来切换窗口。

      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);
      

      有关如何处理窗口和弹出窗口的更多信息,请访问以下 URL 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

      【讨论】:

        猜你喜欢
        • 2016-06-05
        • 1970-01-01
        • 2012-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-03
        相关资源
        最近更新 更多