首页 > 解决方案 > Selenium:网页弹出/警报/通知消息的元素不可见异常

问题描述

我正在尝试自动化以下页面上的一个案例:

http://automationpractice.com/index.php?id_product=4&controller=product

有步骤:

Load Page > Click 'Add to cart' > Popup appears with buttons > Press 'Proceed to checkout'

但是,我的代码在单击时失败"elementButton.click()"

我得到的例外:

“线程“主”org.openqa.selenium.ElementNotVisibleException 中的异常:元素不可见“

该元素已启用;但是,它是不可见的,因为它是一个弹出窗口,我必须移动到弹出窗口然后单击签出,或者警报/弹出似乎不起作用。

如果有人请可以提供帮助。提前谢谢了。


driver.get("http://automationpractice.com/index.php?id_product=4&controller=product");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
        driver.findElement(By.xpath(".//*[text()='Add to cart']")).click();

        WebElement elementButton = driver.findElement(By.xpath(".//a[contains(@title,'Proceed to checkout')]"));

        System.out.println(" ****elementButton**********" + elementButton.isEnabled());  // returning true
        elementButton.click();

标签: javaseleniumwebdriver

解决方案


加载页面 > 单击“添加到购物车”> 出现带有按钮的弹出窗口 > 按“继续结帐”,您需要诱导WebDriverWait以使元素可点击,并且您可以使用以下任一定位器策略

  • 选择器

    driver.get("http://automationpractice.com/index.php?id_product=4&controller=product");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("p#add_to_cart>button span"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[title='Proceed to checkout']>span"))).click();
    
  • 路径

    driver.get("http://automationpractice.com/index.php?id_product=4&controller=product");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Add to cart']"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@title='Proceed to checkout']/span"))).click();
    
  • 浏览器快照:

自动化实践

您可以在 org.openqa.selenium.ElementNotVisibleException 中找到详细讨论:通过 SeleniumWebDriver 和 Java 单击复选框时元素当前不可见


推荐阅读