Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何检查元素是否可见_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java 如何检查元素是否可见

Java 如何检查元素是否可见,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,需要您的帮助,请检查特定元素是否可见。 在下面的代码中,我传递了错误的ID,因此系统将抛出NoTouchElementException,如果元素正确,系统将给出正确的答案。但在错误元素的情况下,它会抛出异常,而不是处理异常 请在这方面提供帮助- package com; import java.util.NoSuchElementException; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; i

需要您的帮助,请检查特定元素是否可见。 在下面的代码中,我传递了错误的ID,因此系统将抛出
NoTouchElementException
,如果元素正确,系统将给出正确的答案。但在错误元素的情况下,它会抛出异常,而不是处理异常

请在这方面提供帮助-

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.
                    }



        }



}

您导入了错误的NoTouchElementException。你应该进口的

org.openqa.selenium.NoSuchElementException


您没有导入java.util.NoTouchElementException,而是导入了错误的NoTouchElementException。你应该进口的

org.openqa.selenium.NoSuchElementException


或者,您可以使用driver.findelelements来确定元素是否存在于页面上,而无需使用NoSuchElementException,而不是
java.util.NoSuchElementException

为此,您可以使用:

private boolean existsElement(String id) {
    return !driver.findElements(By.name(id)).isEmpty();
}

当findElements方法找不到任何与指定定位器匹配的元素时,它将返回一个空列表。它是捕获NoSuchElementException的一种非常常见的替代方法。

或者,您可以使用driver.findElements作为一种方法来确定元素是否存在于页面上,而无需使用NoSuchElementException

为此,您可以使用:

private boolean existsElement(String id) {
    return !driver.findElements(By.name(id)).isEmpty();
}

当findElements方法找不到任何与指定定位器匹配的元素时,它将返回一个空列表。它是捕获NoSuchElementException的一个非常常见的替代方法。

虽然捕获NoSuchElementException有效,但它不是一个优雅的解决方案。想象一下,如果你必须在多个地方使用这种逻辑。代码将变得臃肿,难以维护。您可以使用类中的帮助器方法。这就是你使用它的方式

WebDriverWait wait = new WebDriverWait(driver,30);

boolean isNotVisible = wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("foo")));

if(isNotVisible) {
   // do stuff
}

虽然捕获
NoTouchElementException
有效,但这并不是一个优雅的解决方案。想象一下,如果你必须在多个地方使用这种逻辑。代码将变得臃肿,难以维护。您可以使用类中的帮助器方法。这就是你使用它的方式

WebDriverWait wait = new WebDriverWait(driver,30);

boolean isNotVisible = wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("foo")));

if(isNotVisible) {
   // do stuff
}

谢谢Vivek…你能帮我用sendkeys上传文件的脚本吗。我在这个网址上问了这个问题。我们已经在邮件中向您提供了解决方案。嗨,Vivek,我相信您可以在这个问题上提供帮助。看看你是否能在这方面帮助我…请Ethanks Vivek…你也能在使用sendkeys上传文件的脚本中帮助我吗。我在这个网址上问了这个问题。我们已经在邮件中向您提供了解决方案。嗨,Vivek,我相信您可以在这个问题上提供帮助。看看你能不能帮我…拜托