首页 > 解决方案 > 仅当它显示在浏览器 Selenium java 中时如何单击警报?

问题描述

这是测试场景

这是该警报的 HTML 代码

<div class="top-alert hidden-xs hidden-sm">
    <div id="alert0" class="alert pull-right alert-danger alert-dismissable" style="display:none;">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>Error:</strong> Invalid Inputs!
    </div>
    <div id="alert2" class="alert pull-right alert-warning alert-dismissable" style="display:none;">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>Warning:</strong> Your Login is limited
    </div>
    <div id="alert3" class="alert pull-right alert-warning alert-dismissable" style="display:none;">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>Warning:</strong> You are Already Loggedin <a href="http://localhost/myapp/public/logout">Logout</a>
    </div>
</div>

这是网站截图

这是我的解决方案

public class Test
{
    public static void main(String[] args)
    {
        System.setProperty("webdriver.chrome.driver", "C:\\\\Selenium\\\\drivers\\\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://localhost/myapp/public/login");

        // Maxmimize windows
        driver.manage().window().maximize();

        // Login the Tripmaker
        driver.findElement(By.xpath("//input[@id='email']")).sendKeys("test@test.com");
        driver.findElement(By.id("password")).sendKeys("s123456");
        driver.findElement(By.cssSelector("#lsbt")).click();

        // Explicit wait
        if (!driver.findElement(By.xpath("//div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')]")).isDisplayed())
        {
            WebDriverWait alert = new WebDriverWait(driver, 5);
            alert.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')]")));
            driver.findElement(By.xpath("//div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')]")).click();
        }
        else if (driver.findElement(By.xpath("//div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')]")).isDisplayed()) 
        {           
            System.out.println("This step is skipped");
        }
    }
}

我的代码正在运行,但是如果在登录时显示警报,则会引发错误

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')] (tried for 5 second(s) with 500 milliseconds interval)
  at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:113)
  at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:283)
  at demo.myapp.main(myapp.java:41)
Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.xpath: //div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')]

我试过了.isDisplayed().isSelected().isEnabled ()

标签: javaselenium-webdriver

解决方案


我在您的代码中修复了一些问题。您混合了By.id()By.cssSelector()By.XPath()所有具有 ID 的查找元素。只要 ID 可用并使用By.id(). 你还有很多多余if的 s 是不必要的。您检查了元素是否不可见,然后等待它可见,然后再次找到它并单击它,否则如果它不可见,则打印一条消息。总而言之,当您只需要执行一次时,您最终会找到该元素 4 次。

由于该元素可能存在也可能不存在,我们需要等待它,因此您需要用try-catch.

public class Test
{
    public static void main(String[] args)
    {
        System.setProperty("webdriver.chrome.driver", "C:\\\\Selenium\\\\drivers\\\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://localhost/myapp/public/login");

        // Maxmimize windows
        driver.manage().window().maximize();

        // Login the Tripmaker
        driver.findElement(By.id("email")).sendKeys("test@test.com");
        driver.findElement(By.id("password")).sendKeys("s123456");
        driver.findElement(By.id("lsbt")).click();

        try
        {
            // wait for the logout link in the dialog and click it
            new WebDriverWait(driver, 3).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#alert3 a[href$='logout']"))).click();
        }
        catch (TimeoutException)
        {
            // logout link was never displayed
            System.out.println("This step is skipped");
        }
    }
}

推荐阅读