繁体   English   中英

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

[英]How to click the alert only if it is displayed in Browser Selenium java?

这是测试方案

  • 步骤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>

这是网站截图

这是我的解决方案

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

您可以像这样通过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");
}

根据您的用 ,等待带有退出链接的警报出现,然后单击带有文本的链接作为退出,您需要诱导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");
}

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

由于该元素可能存在或可能不存在,并且我们需要等待它,因此您需要在try-catch等待中加上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");
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM