这是测试场景
这是该警报的HTML CODE
<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">×</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">×</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">×</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("[email protected]");
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");
}
根据您的用例,等待警报显示为注销链接并单击带有文本的链接作为Logout,您需要引导WebDriverWait以使元素可单击,您可以使用以下代码块:
//Login the Tripmaker
driver.findElement(By.xpath("//input[@id='email']")).sendKeys("[email protected]");
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()
。你还有很多额外的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("[email protected]");
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");
}
}
}