【问题标题】:Unable to switch window in Selenium web driver无法在 Selenium Web 驱动程序中切换窗口
【发布时间】:2013-12-23 07:12:54
【问题描述】:

我有以下作业:

  • 使用 Selenium Webdriver 编写程序
  • 在 Firefox 中打开“parent.htm”文件。
  • 然后单击“打开模式弹出窗口”按钮。
  • 在弹出窗口中,再次单击“打开模式弹出窗口”。
  • 通过单击弹出窗口中的关闭按钮关闭所有窗口

到目前为止完成的代码:

import java.util.List;

import java.util.Set;



import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.internal.seleniumemulation.JavascriptLibrary;



public class popup_test {

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

parent();

}

public static void parent() throws InterruptedException{

System.setProperty("webdriver.chrome.driver","C:\\jars\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

//WebDriver driver = new FirefoxDriver();

driver.get("C:\\Users\\jain\\Desktop\\popup\\parent.htm");

Thread.sleep(1000);

String winHandleBefore1 = driver.getWindowHandle();

driver.findElement(By.id("OpenPopupButton")).click();

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

Object[] ObjHwndvals=winHandleColl_After.toArray();

Object Objhwnd=ObjHwndvals[1];

String strHwnd=(String) Objhwnd;

driver=driver.switchTo().window(strHwnd);

driver.findElement(By.id("OpenPopupButton")).click();



winHandleColl_After = driver.getWindowHandles();

ObjHwndvals=winHandleColl_After.toArray();

Object Objhwnd3=ObjHwndvals[2];

String strHwnd3=(String) Objhwnd;

driver.switchTo().window(strHwnd3).findElement(By.id("MyModalPopup")).click();

driver.switchTo().window(strHwnd).findElement(By.id("MyModalPopup")).click();

}

文件:请将其另存为 .htm 扩展名 家长:

<html>
<title>Parent Window</title>

<script type="text/javascript" language="javascript">

   function openPopUpWithSize(theURL, title, heightInPX, widthInPX)
   {
   window.showModalDialog(theURL, title, 'dialogHeight=' + heightInPX + 'px;dialogWidth='+ widthInPX + 'px');
   }
</script>

<body>

<div>
<input type="button" id="OpenPopupButton" value="Open Modal Popup" onclick="openPopUpWithSize('child.htm', 'Simple Modal Popup Example', 400, 500);" />
</div>
</body>
</html>

孩子:

<html>
<title>Child Window</title>

<script type="text/javascript" language="javascript">

  function openPopUpWithSize(theURL, title, heightInPX, widthInPX)
  {
    window.showModalDialog(theURL, title, 'dialogHeight=' + heightInPX + 'px;dialogWidth='+ widthInPX + 'px');
  }
  function closePopup() {
    window.close();
    return false;
  }

</script>

<body>

<div>
<input type="button" id="OpenPopupButton" value="Open Modal Popup" onclick="openPopUpWithSize('child.htm', 'Simple Modal Popup Example', 400, 500);" />

<input type="button" id="MyModalPopup" value="Close" onclick="closePopup();" />
</div>

</body>
</html>

【问题讨论】:

  • 你想做什么,关闭所有弹出窗口后你想切换你的父窗口。我说的对吗?
  • 是的,Naveen,这正是我想要的
  • 比使用 [driver.switchTo().window(winHandleBefore1);] 这里的“winHandleBefore1”字符串包含父窗口状态。

标签: java javascript html selenium


【解决方案1】:

您使用的 showModalDialog() 方法是特定于 IE 浏览器的,因此最好使用 IE 浏览器尝试。

【讨论】:

    猜你喜欢
    • 2012-07-06
    • 2018-07-15
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    相关资源
    最近更新 更多