Java 无法在Selenium web驱动程序中切换窗口
我有以下作业:Java 无法在Selenium web驱动程序中切换窗口,java,javascript,html,selenium,Java,Javascript,Html,Selenium,我有以下作业: 使用SeleniumWebDriver编写程序 在Firefox中打开“parent.htm”文件 然后单击“打开模式弹出窗口”按钮 在弹出窗口中,再次单击“打开模式弹出窗口” 单击弹出窗口中的关闭按钮关闭所有窗口 迄今为止完成的代码: import java.util.List; import java.util.Set; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExe
- 使用SeleniumWebDriver编写程序
- 在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();
}
import java.util.List;
导入java.util.Set;
导入org.openqa.selenium.By;
导入org.openqa.selenium.JavascriptExecutor;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.openqa.selenium.firefox.FirefoxDriver;
导入org.openqa.selenium.internal.seleniumemulation.JavascriptLibrary;
公共类测试{
公共静态void main(字符串args[])引发InterruptedException{
父项();
}
public static void parent()引发InterruptedException{
System.setProperty(“webdriver.chrome.driver”,“C:\\jars\\chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
//WebDriver=newfirefoxdriver();
get(“C:\\Users\\jain\\Desktop\\popup\\parent.htm”);
睡眠(1000);
字符串winHandleBefore1=driver.getWindowHandle();
driver.findElement(By.id(“OpenPopupButton”)).click();
设置winHandleColl_After=driver.getWindowHandles();
Object[]ObjHwndvals=winHandleColl_After.toArray();
对象Objhwnd=ObjHwndvals[1];
字符串strHwnd=(字符串)Objhwnd;
driver=driver.switchTo().window(strHwnd);
driver.findElement(By.id(“OpenPopupButton”)).click();
winHandleColl_After=driver.getWindowHandles();
ObjHwndvals=winHandleColl_在.toArray()之后;
对象Objhwnd3=ObjHwndvals[2];
字符串strHwnd3=(字符串)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>
父窗口
函数openPopUpWithSize(URL、标题、高度inpx、宽度inpx)
{
showModalDialog(URL,标题,'dialogHeight='+heightInPX+'px;dialogWidth='+widthInPX+'px');
}
儿童:
<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>
子窗口
函数openPopUpWithSize(URL、标题、高度inpx、宽度inpx)
{
showModalDialog(URL,标题,'dialogHeight='+heightInPX+'px;dialogWidth='+widthInPX+'px');
}
函数closePopup(){
window.close();
返回false;
}
您使用的showModalDialog()方法是IE浏览器特有的,因此最好尝试使用IE浏览器。您好,关闭所有弹出窗口后,您想做什么?我说的对吗?是的,Naveen,这正是我想要使用的[driver.switch to().window(winHandleBefore1);]这里是“winHandleBefore1”字符串包含父窗口状态。