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