Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.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 如何验证元素在selenium 2中是否存在或可见(selenium WebDriver)_Java_Selenium Webdriver_Webdriver - Fatal编程技术网

Java 如何验证元素在selenium 2中是否存在或可见(selenium WebDriver)

Java 如何验证元素在selenium 2中是否存在或可见(selenium WebDriver),java,selenium-webdriver,webdriver,Java,Selenium Webdriver,Webdriver,任何人都可以给我发送如何验证元素的示例代码 i呈现 isvisible isenable text当前 在使用Java的Selenium WebDrvier中,您可以尝试以下操作: WebElement rxBtn = driver.findElement(By.className("icon-rx")); WebElement otcBtn = driver.findElement(By.className("icon-otc")); WebElement herbBt

任何人都可以给我发送如何验证元素的示例代码

  • i呈现
  • isvisible
  • isenable
  • text当前

  • 在使用Java的Selenium WebDrvier中,您可以尝试以下操作:

        WebElement rxBtn = driver.findElement(By.className("icon-rx"));
        WebElement otcBtn = driver.findElement(By.className("icon-otc"));
        WebElement herbBtn = driver.findElement(By.className("icon-herb"));
    
        Assert.assertEquals(true, rxBtn.isDisplayed());
        Assert.assertEquals(true, otcBtn.isDisplayed());
        Assert.assertEquals(true, herbBtn.isDisplayed());
    

    这只是一个例子。基本上,您可以声明和定义希望使用的WebElement变量,然后断言它们是否显示。这是使用TestNG断言。

    要确保元素存在,可以执行以下操作:

    driver.findElements(By.id("id"));
    
    这将返回一个数组,如果该数组大小大于0,则该元素存在

    此外,你还需要提供更多的信息,比如语言和在提问之前你试过什么


    祝你好运

    为了便于理解,我使用了java打印语句

  • 要检查存在的元素,请执行以下操作:

    if(driver.findElements(By.xpath("value")).size() != 0){
    System.out.println("Element is Present");
    }else{
    System.out.println("Element is Absent");
    }
    

  • 要检查是否可见,请执行以下操作:

    if( driver.findElement(By.cssSelector("a > font")).isDisplayed()){
    System.out.println("Element is Visible");
    }else{
    System.out.println("Element is InVisible");
    }
    
  • 要选中“启用”,请执行以下操作:

    if( driver.findElement(By.cssSelector("a > font")).isEnabled()){
    System.out.println("Element is Enable");
    }else{
    System.out.println("Element is Disabled");
    }
    
  • 检查文本是否存在

    if(driver.getPageSource().contains("Text to check")){
    System.out.println("Text is present");
    }else{
    System.out.println("Text is absent");
    }
    

  • 这是我的SeleniumWebDriver的Java代码。编写以下方法并在断言期间调用它:

    protected boolean isElementPresent(By by){
            try{
                driver.findElement(by);
                return true;
            }
            catch(NoSuchElementException e){
                return false;
            }
        }
    
    尝试使用以下代码:

    private enum ElementStatus{
            VISIBLE,
            NOTVISIBLE,
            ENABLED,
            NOTENABLED,
            PRESENT,
            NOTPRESENT
        }
        private ElementStatus isElementVisible(WebDriver driver, By by,ElementStatus getStatus){
            try{
                if(getStatus.equals(ElementStatus.ENABLED)){
                    if(driver.findElement(by).isEnabled())
                        return ElementStatus.ENABLED;
                    return ElementStatus.NOTENABLED; 
                }
                if(getStatus.equals(ElementStatus.VISIBLE)){
                    if(driver.findElement(by).isDisplayed())
                        return ElementStatus.VISIBLE;
                    return ElementStatus.NOTVISIBLE;
                }
                return ElementStatus.PRESENT;
            }catch(org.openqa.selenium.NoSuchElementException nse){
                return ElementStatus.NOTPRESENT;
            }
        }
    

    要检查元素是否可见,我们需要使用
    element.isDisplayed()
    但如果我们需要检查Dom中任何位置的元素是否存在,我们可以使用以下方法

    public boolean isElementPresentCheckUsingJavaScriptExecutor(WebElement element) {
            JavascriptExecutor jse=(JavascriptExecutor) driver;
            try {
                Object obj = jse.execute("return typeof(arguments[0]) != 'undefined' && arguments[0] != null;",
                        element);
                if (obj.toString().contains("true")) {
                    System.out.println("isElementPresentCheckUsingJavaScriptExecutor: SUCCESS");
                    return true;
                } else {
                    System.out.println("isElementPresentCheckUsingJavaScriptExecutor: FAIL");
                }
    
            } catch (NoSuchElementException e) {
                System.out.println("isElementPresentCheckUsingJavaScriptExecutor: FAIL");
            }
            return false;
        }
    

    你试过什么?你读过任何教程吗?用什么语言?C#?JAVAPHP?猪拉丁语?我想用java语言试试。请向我发送selenium web驱动程序的教程或帮助文档。我会感谢你的。请帮助mei尝试上面的示例,但如果前面的断言失败,则不会执行下一步。我想使用verify而不是断言,这样它就可以运行下一个测试步骤。如果断言和verficatin中的任何一个失败了,我也会使整个测试用例失败,所以我从您的回复中得到的信息是,即使断言失败了,您也希望测试继续进行。是吗?是的,我要的正是你写的。我想让测试继续进行,即使测试断言是失败的,并且在所有测试步骤执行之后的最后,在报告中将测试结果标记为失败结果。但它应该完成所有步骤的执行。您使用的是什么语言?如果您使用的是Java TestNG或JUnit?根据您运行测试的方式,我在TestNG文档中找到的唯一选项是命令行参数的
    -configurefailpolicy
    。driver.findelement(By.id(“id1”)).isDisplayed()'driver.findelement(By.id(“id2”)).sendkey(“测试”);在这里,若并没有显示元素,那个么它将使测试用例失败,并且不会继续执行下一步。因此,请帮助我如何处理SeleniumWebDrider和testng中接下来步骤的断言和xcontinue测试执行。即使我也希望在任何断言失败时标记测试用例失败。id是唯一的,您应该使用.findElement()进行标记。findElements()用于例如By.ClassName()这些示例最好使用ExpectedConditions类。if(driver.findElement(By.cssSelector(“a>font”)).isDisplayed(){将不起作用,因为isDisplayed不会返回false。仅当显示时返回true,否则会引发element not found异常…方法execute(String,WebElement)未为类型JavascriptExecutor定义
    public boolean isElementPresentCheckUsingJavaScriptExecutor(WebElement element) {
            JavascriptExecutor jse=(JavascriptExecutor) driver;
            try {
                Object obj = jse.execute("return typeof(arguments[0]) != 'undefined' && arguments[0] != null;",
                        element);
                if (obj.toString().contains("true")) {
                    System.out.println("isElementPresentCheckUsingJavaScriptExecutor: SUCCESS");
                    return true;
                } else {
                    System.out.println("isElementPresentCheckUsingJavaScriptExecutor: FAIL");
                }
    
            } catch (NoSuchElementException e) {
                System.out.println("isElementPresentCheckUsingJavaScriptExecutor: FAIL");
            }
            return false;
        }
    
    webDriver.findElement(By.xpath("//*[@id='element']")).isDisplayed();