2012-12-18 7 views
11

मैं दिए गए उदाहरण के मॉडल संवाद करने के लिए स्विच करने में असमर्थ हूँसेलेनियम वेबड्राइवर का उपयोग करके मॉडेलडियलॉग से कैसे निपटें?

http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog2.htm

मैं कैसे मॉडल संवाद पर तत्व प्राप्त करने के लिए

enter image description here

+0

चेक कैसे selenium2.0 का उपयोग कर मोडल संवाद या पॉप अप को संभालने के लिए की बेहतर समझ के लिए नीचे दिए गए लिंक http://www.thoughtworks-studios.com/twist/2.3/help/how_do_i_handle_popup_in_selenium2.html – Hemanth

उत्तर

15

उपयोग

निम्न विधियों में पता नहीं है modelframe

driver.switchTo().frame("ModelFrameTitle"); 
पर स्विच करने के लिए

या

driver.switchTo().activeElement() 

आशा इस काम करेंगे

+0

मैंने दोनों की कोशिश की है लेकिन इनमें से कोई भी काम नहीं कर रहा है। क्या आप इसे आज़मा सकते हैं और मुझे –

+2

@ जैस्मीन.ऑलिव्रा बता सकते हैं, आपने जवाब स्वीकार कर लिया है, हालांकि एक ही समय में कहा गया था कि "* इनमें से कोई भी काम नहीं कर रहा है"। तो क्या यह आपके लिए काम कर रहा है या नहीं? –

+0

मैं पाइथन में ऐसा कर रहा हूं, लेकिन मेरा कथन है: driver.switch_to_window (activeElement()) .... हालांकि मुझे त्रुटि मिल रही है: driver.switch_to_window (activeElement())। क्या आप कोई संकेत दे सकते हैं? – engineer

0

इस कोड का प्रयास करें, अपने वस्तु के नाम से काम करने के & चर शामिल हैं।

Set<String> windowids = driver.getWindowHandles(); 
Iterator<String> iter= windowids.iterator(); 
for (int i = 1; i < sh.getRows(); i++) 
{ 
while(iter.hasNext()) 
{ 
System.out.println("Main Window ID :"+iter.next()); 
} 
driver.findElement(By.id("lgnLogin_UserName")).clear(); 
driver.findElement(By.id("lgnLogin_UserName")).sendKeys(sh.getCell(0, 
i).getContents()); 
driver.findElement(By.id("lgnLogin_Password")).clear(); 
driver.findElement(By.id("lgnLogin_Password")).sendKeys(sh.getCell(1, 
i).getContents()); 
driver.findElement(By.id("lgnLogin_LoginButton")).click(); 
Thread.sleep(5000L); 
      windowids = driver.getWindowHandles(); 
    iter= windowids.iterator(); 
    String main_windowID=iter.next(); 
    String tabbed_windowID=iter.next(); 
    System.out.println("Main Window ID :"+main_windowID); 
    //switch over to pop-up window 
    Thread.sleep(1000); 
    driver.switchTo().window(tabbed_windowID); 
    System.out.println("Pop-up window Title : "+driver.getTitle()); 
1

आप जो भी उपयोग कर रहे हैं वह एक मॉडल संवाद नहीं है, यह एक अलग खिड़की है। इस कोड को

उपयोग:

private static Object firstHandle; 
private static Object lastHandle; 

public static void switchToWindowsPopup() { 
    Set<String> handles = DriverManager.getCurrent().getWindowHandles(); 
    Iterator<String> itr = handles.iterator(); 
    firstHandle = itr.next(); 
    lastHandle = firstHandle; 
    while (itr.hasNext()) { 
     lastHandle = itr.next(); 
    } 
    DriverManager.getCurrent().switchTo().window(lastHandle.toString()); 
} 

public static void switchToMainWindow() { 
    DriverManager.getCurrent().switchTo().window(firstHandle.toString()); 
0

मैं इसे की कोशिश की है, यह आप के लिए काम करता है।

String mainWinHander = webDriver.getWindowHandle(); 

// code for clicking button to open new window is ommited 

//Now the window opened. So here reture the handle with size = 2 
Set<String> handles = webDriver.getWindowHandles(); 

for(String handle : handles) 
{ 
    if(!mainWinHander.equals(handle)) 
    { 
     // Here will block for ever. No exception and timeout! 
     WebDriver popup = webDriver.switchTo().window(handle); 
     // do something with popup 
     popup.close(); 
    } 
} 
1

Try the below code. It is working in IE but not in FF22. Ifमॉडल संवादis printed in Console, then Modal dialog is identified and switched.

public class ModalDialog { 

     public static void main(String[] args) throws InterruptedException { 
      // TODO Auto-generated method stub 
      WebDriver driver = new InternetExplorerDriver(); 
      //WebDriver driver = new FirefoxDriver(); 
      driver.get("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog2.htm"); 
      String parent = driver.getWindowHandle(); 
      WebDriverWait wait = new WebDriverWait(driver, 10); 
      WebElement push_to_create = wait.until(ExpectedConditions 
        .elementToBeClickable(By 
          .cssSelector("input[value='Push To Create']"))); 
      push_to_create.click(); 
      waitForWindow(driver); 
      switchToModalDialog(driver, parent); 

     } 

     public static void waitForWindow(WebDriver driver) 
       throws InterruptedException { 
      //wait until number of window handles become 2 or until 6 seconds are completed. 
      int timecount = 1; 
      do { 
       driver.getWindowHandles(); 
       Thread.sleep(200); 
       timecount++; 
       if (timecount > 30) { 
        break; 
       } 
      } while (driver.getWindowHandles().size() != 2); 

     } 

     public static void switchToModalDialog(WebDriver driver, String parent) { 
       //Switch to Modal dialog 
      if (driver.getWindowHandles().size() == 2) { 
       for (String window : driver.getWindowHandles()) { 
        if (!window.equals(parent)) { 
         driver.switchTo().window(window); 
         System.out.println("Modal dialog found"); 
         break; 
        } 
       } 
      } 
     } 

    } 
0

पाया मान लिया जाये कि उम्मीद सिर्फ (पॉपअप के लिए माता-पिता में से एक और एक) ऊपर पॉपिंग तो बस के लिए इंतजार दो खिड़कियां होने जा रहा है दो खिड़कियां आने के लिए, दूसरी विंडो हैंडल ढूंढें और उस पर स्विच करें।

WebElement link = // element that will showModalDialog() 

// Click on the link, but don't wait for the document to finish 
final JavascriptExecutor executor = (JavascriptExecutor) driver; 
executor.executeScript(
    "var el=arguments[0]; setTimeout(function() { el.click(); }, 100);", 
    link); 

// wait for there to be two windows and choose the one that is 
// not the original window 
final String parentWindowHandle = driver.getWindowHandle(); 
new WebDriverWait(driver, 60, 1000) 
    .until(new Function<WebDriver, Boolean>() { 
    @Override 
    public Boolean apply(final WebDriver driver) { 
     final String[] windowHandles = 
      driver.getWindowHandles().toArray(new String[0]); 
     if (windowHandles.length != 2) { 
      return false; 
     } 
     if (windowHandles[0].equals(parentWindowHandle)) { 
      driver.switchTo().window(windowHandles[1]); 
     } else { 
      driver.switchTo().window(windowHandles[0]); 
     } 
     return true; 
    } 
}); 
संबंधित मुद्दे