java - 仅当它显示在浏览器 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">×</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>
这是网站截图
这是我的解决方案
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 ()
。
解决方案
我在您的代码中修复了一些问题。您混合了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");
}
}
}
推荐阅读
- d3.js - 自定义 Apache 超集中的数字格式
- r - 为什么 knitr 会在其他工作正常的管道上窒息?
- node.js - NestJs 请求标头大小错误 Http 代码 431
- python - py2app 无法启动,系统控制台中没有重要日志
- javascript - 验证表单上的多个字段类型
- javascript - 从 javascript 数据集中删除一行并将其转移到另一个数据集中?
- javascript - 获取 React 组件名称的最佳方法是什么?
- node.js - 一个信号创建通知 REST API 有时会失败
- node.js - 如何正确地将读取流分叉为 3 个压缩流以进行基准计算?
- python-3.x - AWS boto3 - 资源:lakeformation 功能:batch_revoke_permissions - 代表 ALL_TABLES