需要您的帮助想要检查特定元素是否可见。
在下面的代码中,我传递了错误的 ID,以便系统在元素正确的情况下抛出 NoSuchElementException
,并给出正确的答案。但如果元素错误,它会抛出异常而不是处理它。
请帮忙 -
package com;
import java.util.NoSuchElementException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class Windowgoogle {
static WebDriver driver;
static String baseUrl="http:/www.google.co.in";
@Test
public void openBrowser()
{
driver=new FirefoxDriver();
driver.get(baseUrl);
System.out.println(existsElement("qo"));//Adding Invalid ID
}
private boolean existsElement(String id)
{
boolean chk = false;
try {
chk=driver.findElement(By.name(id)).isDisplayed();
return chk;
} catch (NoSuchElementException e)
{
return false;//Control should go to catch but exception is not getting handled properly.
}
}
}
最佳答案
您导入了错误的NoSuchElementException。您应该导入
org.openqa.selenium.NoSuchElementException
而不是java.util.NoSuchElementException
关于java - 如何检查元素是否可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28613755/