简体   繁体   English

如何在 html 标签中不存在元素时处理 Selenium findElements

[英]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.

相关问题 findelements中的硒(java)findelements - selenium (java) findelements in findelements 如何使用 Selenium 在 HTML 标记中查找没有值的属性是否存在? - How to find attribute without value is present or not in HTML tag using Selenium? Selenium WebDriver- FindElements返回未显示的元素 - Selenium WebDriver- FindElements returns elements which are not displayed Selenium findElements() 是否必须隐式等待返回 0 个元素? - Does Selenium findElements() have to implicitly wait to return 0 elements? Selenium-Java-按类名查找元素不会返回所有元素 - Selenium - Java - findelements by classname does not return all elements 如何在Selenium Webdriver中使用Java验证HTML代码中是否存在用于下拉列表的标记 - How to verify whether a tag is present in the html code for a drop down using java in Selenium Webdriver 如何编写定位器并获取 selenium 自动化代码 HTML 代码中单个标签中的文本 - How to write a locator and get both the text present in a single tag in HTML code for selenium automation Selenium 处理对象标签内的 html 元素 - Selenium handling html elements inside object tag Selenium FindElements 方法 Java - Selenium FindElements method Java Selenium:如何判断RemoteWebDriver.findElements(By)是否可以引发StaleElementReferenceException? - Selenium: How to tell if RemoteWebDriver.findElements(By) can throw StaleElementReferenceException at all?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM