[英]How to check if element is visible or not
Need your help wanted to check if particular element is visible or not. 需要您的帮助以检查特定元素是否可见。 in the below code I have passed wrong ID so that system will throw
NoSuchElementException
in the case of correct element it is giving correct answer. 在下面的代码中,我传递了错误的ID,以便在元素正确给出正确答案的情况下,系统将引发
NoSuchElementException
。 But in the case of wrong element it is throwing exception instead of handling it. 但是在元素错误的情况下,它抛出异常而不是处理它。
Please help in this - 请帮忙-
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.
}
}
}
You have imported wrong NoSuchElementException. 您导入了错误的NoSuchElementException。 You should have imported
您应该已经导入
org.openqa.selenium.NoSuchElementException
org.openqa.selenium.NoSuchElementException
instead of java.util.NoSuchElementException
而不是
java.util.NoSuchElementException
Alternatively, you are able to use driver.findElements as a way to determine if the element is present on the page without using the NoSuchElementException. 另外,您可以使用driver.findElements作为确定元素是否在页面上的方式,而无需使用NoSuchElementException。
For this you would use: 为此,您将使用:
private boolean existsElement(String id) {
return !driver.findElements(By.name(id)).isEmpty();
}
When the method findElements cannot find any elements that match the specified locator, it returns an empty list. 当findElements方法找不到与指定定位符匹配的任何元素时,它将返回一个空列表。 It is a very common alternative to catching the NoSuchElementException.
这是捕获NoSuchElementException的非常常见的替代方法。
While catching NoSuchElementException
works, it's not an elegant solution. 捕获
NoSuchElementException
可以正常工作,但这不是一个很好的解决方案。 Imagine if you have to have this logic in multiple places. 想象一下,如果您必须在多个地方使用此逻辑。 The code will be bloated and hard to maintain.
该代码将肿且难以维护。 You can use helper methods from ExpectedConditions class.
您可以使用ExpectedConditions类中的帮助器方法。 This is how you would use it,
这就是您的使用方式,
WebDriverWait wait = new WebDriverWait(driver,30);
boolean isNotVisible = wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("foo")));
if(isNotVisible) {
// do stuff
}
import java.util.NoSuchElementException;
import org.openqa.selenium.*;
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 dropdown {
private static WebDriver driver = new FirefoxDriver() ;
String Path ="C:\\Users\\VGupta\\Desktop\\testcases\\auto.xlsx";
@Test
public void test() throws Exception
{
driver.get("http://www.test.com/");
//Dimension size = ('900','500');
driver.manage().window().setSize(new Dimension(1000,1000));
try{
driver.findElement(By.id("foo")).click();
} catch(NoSuchElementException e)
{
System.out.println(e.getStackTrace());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.