我有以下作业:
- 使用Selenium Webdriver编写程序
- 在Firefox中打开“parent.htm”文件。
- 然后单击“打开模态弹出窗口”按钮。
- 在弹出窗口中,再次单击“打开模态弹出窗口”。
- 点击Pop-Ups
中的关闭按钮关闭所有窗口
到目前为止完成的代码:
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>
答案 0 :(得分:0)
您使用的方法showModalDialog()特定于IE浏览器,因此最好尝试使用IE浏览器。