简体   繁体   English

如果对象在Selenium Webdriver Java中不可见,如何使用xpath验证对象

[英]how to verify object using xpath if object is invisible in selenium webdriver java

How can I verify invisible objects which gets enabled only on incorrect data entry (label before textbox eg: login scenario) using xpath in selenium webdriver. 我如何使用Selenium Webdriver中的xpath验证仅在不正确的数据输入(文本框前的标签,例如:登录方案)上启用的不可见对象。

If all labels display with error message (eg username label and password label),I am able to verify and capture text (error messages). 如果所有标签均显示错误消息(例如,用户名标签和密码标签),则我可以验证和捕获文本(错误消息)。 xpath Username: .// [@id='loginForm']/p[1]/p/label xpath Password: .// [@id='loginForm']/p[2]/p/label xpath用户名:.// [@ id ='loginForm'] / p [1] / p / label xpath密码:.// [@ id ='loginForm'] / p [2] / p / label

But if I enter correct Username,Webdriver continue searching for that Xpath for username(.//*[@id='loginForm']/p[1]/p/label) and does not respond. 但是,如果输入正确的用户名,Webdriver将继续在Xpath中搜索用户名(.//*[@id='loginForm']/p[1]/p/label),并且不会响应。

HTML are visible only if error appears: label class="error" generated="true" for="userName" label class="error" generated="true" for="passwordField HTML仅在出现错误时可见:label class =“ error” generate =“ true” for =“ userName” label class =“ error” Generated =“ true” for =“ passwordField

Can anyone help me with this? 谁能帮我这个?

Use a WebDriverWait: 使用WebDriverWait:

WebDriverWait waiting = new WebDriverWait(driver, 15, 100);
waiting.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[@id='loginForm']/p[1]/p/label")));

The above will create a web driver wait object that will wait for up to 15 second (checking the condition every 100ms) for the element to become invisible. 上面的代码将创建一个Web驱动程序等待对象,该对象最多等待15秒(每100毫秒检查一次条件),以使该元素不可见。 This will pass straight away if the element is already invisible when the line of code is triggered. 如果在触发代码行时该元素已经不可见,则将立即通过。

Edit based upon comments below 根据以下评论进行编辑

If you are just trying to find out if an element exists of not you can do: 如果您只是想找出不存在的元素,则可以执行以下操作:

List<WebElement> labelCount = driver.findElements(By.xpath("//*[@id='loginForm']/p[1]/p/label"));

If the size is 0 the label isn't there, else it is there, so something like: 如果大小为0,则标签不存在,否则标签在那里,因此类似:

if(labelCount.size() == 0){
    //Get tab text
} else {
    //Enter second field data
}

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

相关问题 如何在 Selenium webdriver 中为 object 获取广义 xpath - how to get generalized xpath for object in Selenium webdriver 如何使用Java验证Selenium WebDriver中的tooltipText - How to verify tooltipText in Selenium WebDriver using Java 在xpath对象中找不到Selenium Webdriver - Selenium Webdriver in xpath object is not found 如何使用Java使用Selenium WebDriver访问不可见的无序列表元素 - How to access invisible Unordered List element with Selenium WebDriver using Java 如何使用带有Java的Selenium WebDriver在iframe中拖动对象 - How do I drag an object in an iframe using Selenium WebDriver with Java 如何使用java在Selenium WebDriver项目中表示页面对象导航 - How to represent Page Object navigation in a Selenium WebDriver project using java 如何在Selenium Webdriver中的xpath中选择具有动态值的对象 - How to select object with dynamic value in xpath in Selenium Webdriver 如何在网页上使用带有 Java 的 Selenium WebDriver 验证工具提示文本 - How to verify tooltip text using Selenium WebDriver with java on a webpage 使用Java在Selenium WebDriver中发布后,每次如何验证文本? - How to verify text everytime after posting with Selenium WebDriver using java? 如何使用Java使用Selenium WebDriver验证动态库? - How to verify dynamic gallery with Selenium WebDriver using Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM