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();
}
}
最佳答案
出现的弹出窗口是模式而不是窗口,因此您不需要获取 windowHandle。另外,关闭窗口的 xpath 不正确。使用 id 来定位关闭按钮,如下所示:
WebElement closePopUp = driver.findElement(By.id("closeButton"));
closePopUp.click();
更喜欢使用 Id 和 Css 选择器来定位元素。另外,如果您需要切换到窗口或获取该窗口的详细信息,请使用 windowHandle。
希望这有帮助。
关于java - 使用 selenium webdriver junit 测试自动化 saksfifthavenue.com 时无法关闭弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39279440/