java - 使用 selenium webdriver junit 测试自动化 saksfifthavenue.com 时无法关闭弹出窗口

标签 java selenium selenium-webdriver selenium-firefoxdriver

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/

相关文章:

python - 如何通过 XPath 查找具有两个可能类名的元素?

linux - Jenkins -Selenium Webdriver-Java : Error : DevToolsActive port does not exit

c# - 使用 Selenium 使用 WindowHandles 跟踪和遍历选项卡和窗口的最佳方法

java - 如何将 ArrayList 中对象的所有值的总成本相加?

java - 自定义 URLClassLoader,运行时出现 NoClassDefFoundError

java - Android中的加密字符串

python - 使用 selenium 和 python 从 HTML 源页面检索文本

java - 模式 20+99 的正则表达式验证

python - 当不同链接的目标名称不同时在窗口之间切换

ruby - 获取测试自动化屏幕截图时的字符编码问题(损坏的字符)