[英]How to check for visibility of an element in Selenium using POM
I have a page where I need to check the visibility of a button and click it if present, Else skip the step.我有一个页面,我需要检查按钮的可见性并单击它(如果存在),否则跳过该步骤。 Using POM, I have tried the following approaches使用 POM,我尝试了以下方法
#Approach -1 #方法 -1
WebDriver driver;
WebElement searchForButton = driver.findElement(By.xpath("//*@id=\"root\"]/div/div[2]/div/div[2]/div/div[2]/div[2]/div"));
public void buttonValidation() {
searchForButton.click();
}
This approach would fail in the case when the button is not visible.在按钮不可见的情况下,这种方法将失败。
#Approach-2: #方法-2:
@FindBy(xpath= "//*[@id=\"root\"]/div/div[2]/div/div[1]/div/div/div[2]/button[1]/span[1]")
private List<WebElement> elements;
public boolean isElementPresent() {
return elements != null && elements.size()>0 ;
}
public void validateButton() throws InterruptedException {
Thread.sleep(3000);
Boolean b = isElementPresent();
if(b)
{
WebElement btn = driver.findElement(By.xpath("//*@id='root']/div/div[2]/div[2]/div[1]/div/div/div[2]/button[1]")); //JAVA NULL POINTER EXCEPTION HERE
btn.click();
}
}
Using this approach, java.lang.NullPointerException is thrown for btn search使用这种方法,为btn搜索抛出 java.lang.NullPointerException
What would be the correct approach?什么是正确的方法?
You can use this generic method:您可以使用此通用方法:
public boolean clickVisible(By element){
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(element));
driver.findElement(element).click();
return true;
}catch (Throwable t){
return false;
}
}
It will get as a parameter a By locator for an element.它将获得一个元素的 By 定位器作为参数。 In case element found visible it will be clicked and true
will be returned otherwise false
will be returned.如果发现元素可见,则将单击该元素并返回true
否则将返回false
。
So you can use it like this:所以你可以像这样使用它:
if(clickVisible(By.xpath("//*@id='root']/div/div[2]/div[2]/div[1]/div/div/div[2]/button[1]"))){
//your flow for case the element found visible and was clicked
}else{
//your flow for alternative case
}
BTW it's strongly not recommended to use automatically generated locators like this //*@id='root']/div/div[2]/div[2]/div[1]/div/div/div[2]/button[1]
since they are highly breakable and very unreliable.顺便说一句,强烈不建议使用像这样自动生成的定位器//*@id='root']/div/div[2]/div[2]/div[1]/div/div/div[2]/button[1]
因为它们极易碎且非常不可靠。
Google presents these two as well as many other tutorials to learn how to choose a correct, reliable locators. Google提供了这两个以及许多其他教程来学习如何选择正确、可靠的定位器。
Use try catch for these kind of scenarios在这些场景中使用 try catch
try {
WebElement btn = driver.findElement(By.xpath("//*@id='root']/div/div[2]/div[2]/div[1]/div/div/div[2]/button[1]")); //JAVA NULL POINTER EXCEPTION HERE
btn.click();
}
catch (Exception e) { /* do nothing */ }
Your isElementPresent()
should take a list args
, cause you are verifying您的isElementPresent()
应该采用列表args
,因为您正在验证
elements != null && elements.size()>0
so it should look like this :所以它应该是这样的:
public boolean isElementPresent(List<WebElement> elements) {
return elements != null && elements.size()>0 ;
}
and your caller method should look like this :-你的调用者方法应该是这样的:-
public void validateButton() throws InterruptedException {
Thread.sleep(3000);
if(isElementPresent(elements))
{
WebElement btn = driver.findElement(By.xpath("//*@id='root']/div/div[2]/div[2]/div[1]/div/div/div[2]/button[1]")); //JAVA NULL POINTER EXCEPTION HERE
btn.click();
}
}
your first approach can be wrapped inside a try-catch block
:-您的第一种方法可以包含在try-catch block
:-
public void buttonValidation() {
try {
searchForButton.click();
}
catch(Exception e) {
e.printStackTrace();
System.out.println("Something went wrong");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.