[英]How to handle Selenium findElements while elements is not present in html tag
In UI if entered text is invalid then error element/tag displaying on UI as well as in HTML code在 UI 中,如果输入的文本无效,则错误元素/标签显示在 UI 以及 HTML 代码中
<div _ngcasde-ltw-c3="" class="error"> Organization only contain alpha chars or special chars -,. </div>
if enter a valid text then no error element/tag displaying on UI as well as in HTML code,如果输入有效文本,则 UI 以及 HTML 代码中不会显示错误元素/标签,
No error in HTML tag
So while writing selenium webDriver code, I have added below code and it works while error in HTML code (invalid text scenario) but while no error tag in HTML code (valid text scenario), my execution stuck in below code line (elements OR count OR result) and not proceed further.因此,在编写 selenium webDriver 代码时,我添加了下面的代码,它在 HTML 代码中出现错误(无效文本场景)时有效,但在 HTML 代码中没有错误标签或场景标签(有效的行元素文本) OR 结果),不再继续。 it remain showing execution running in that line for unlimited time它仍然显示在该行中无限时间运行的执行
WebElement xyz = driver.findElement(By.className("modal-body"));
List<WebElement> elements = xyz.findElements(By.xpath("//div[@class='error']"));
OR
int count = xyz.findElements(By.xpath("//div[@class='error']")).size();
OR
boolean result = xyz.findElement(By.xpath("//div[@class='error']")).isDisplayed();
So please let me know how to handle this and proceed further, if element not present then it should return size 0 or result as false所以请让我知道如何处理这个问题并继续进行,如果元素不存在,那么它应该返回大小 0 或结果为 false
Based on this output I will use if clause to print message, error if size is not zero and success if size is 0基于此 output 我将使用 if 子句打印消息,如果大小不为零则错误,如果大小为 0 则成功
Declare your error element using FindBy annotation and use it only when the error gets generated on the page.使用FindBy注释声明您的错误元素,并仅在页面上生成错误时使用它。
@FindBy(id = "foobar") WebElement foobar; @FindBy(id = "foobar") WebElement foobar;
Example:例子:
@FindBy(id = "error_css") WebElement errorElement;
public void verifyError(){
// Click on Save button without filling out mandatory fields on the form
save.click();
// Now validate error as it will be generate on the HTML
errorElement.gettext();
}
Happy Testing:)快乐测试:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.