仅当警报显示在Browser Selenium java中时,如何单击该警报?

时间:2018-09-18 12:36:38

标签: java selenium-webdriver

这是测试场景

  • 步骤1:-输入用户名和密码
  • 步骤2:-如果用户已经登录,将显示警报/弹出窗口 带有注销链接,然后单击链接以注销并尝试登录 再次。

这是该警报的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>

这是网站screenshot

这是我的解决方法

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 ()

3 个答案:

答案 0 :(得分:1)

您可以通过try-catch块来处理弹出窗口,

try {
      if(driver.findElement(By.id("alert3")).isDisplayed())
      {
      driver.findElement(By.linkText("Logout")).click();
      }
} catch(Exception e) {
      System.out.println("User defined Message");
}

答案 1 :(得分:1)

根据您的用例,等待带有注销链接的警报出现,然后单击文本链接为注销的链接,您需要诱导 WebDriverWait 使元素可点击,您可以使用以下代码块:

//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();
try {
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='top-alert hidden-xs hidden-sm']//div[@class='alert pull-right alert-warning alert-dismissable' and @id='alert3']//a[@href='http://localhost/myapp/public/logout']"))).click();
}
catch (TimeoutException e) { 
    System.out.println("Element is not present");
}

答案 2 :(得分:1)

我在您的代码中修复了一些问题。您混合使用By.id()By.cssSelector()By.XPath()来查找所有具有ID的元素。只要有ID,就应该使用ID,并使用By.id()。您还拥有许多不必要的额外if。您检查了该元素是否不可见,然后等待它可见,然后再次找到它并单击它,否则,如果它不可见,则打印一条消息。总而言之,您只需要执行一次就可以找到该元素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");
        }
    }
}