-1
我有以下功課:無法在硒的網絡驅動器開關,窗口
- 編寫使用硒的webdriver
- 打開Firefox中的「parent.htm」文件的程序。
- 然後點擊「Open Modal Popup」按鈕。
- 在彈出窗口中,再次點擊「打開模態彈出窗口」。
- 關閉通過點擊彈出窗口
代碼的關閉按鈕,所有窗口完成至今:
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,這正是我想要的 – user3128718
比使用[driver.switchTo()。window(winHandleBefore1);]這裏的「winHandleBefore1」字符串包含父窗口狀態。 –