public class TestScript {
protected WebDriver driver;
@Before
public void setUp() throws MalformedURLException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.navigate().to(new URL("http://www.saksfifthavenue.com"));
}
@Test
public void Test1(){
String currentWindow = driver.getWindowHandle();
//even without using windowhandle the same thing happens. can't locate the popup
WebElement closePopUp = driver.findElement(By.xpath("//div[1][@id='close-button']"));
closePopUp.click();
}
@After
public void tearDown(){
driver.close();
driver.quit();
}
}
答案 0 :(得分:1)
出现的弹出窗口是模态而不是窗口,因此您无需获取windowHandle。此外,关闭窗口的xpath不正确。使用id定位关闭按钮,如下所示:
WebElement closePopUp = driver.findElement(By.id("closeButton"));
closePopUp.click();
首选使用Id和Css选择器来定位元素。此外,在旁注窗口上,如果您需要切换到窗口或获取该窗口的详细信息,则使用手柄。
希望这有帮助。
答案 1 :(得分:0)
试试这段代码:
// Checking Alert is present and switchTo;
Alert alert = new WebDriverWait(driver,30).until(ExpectedConditions.alertIsPresent());
// Dismiss alert - this will close the alert
alert.dismiss();
//Switching to default content after alert is closed
driver.switchTo().defaultContent();