简体   繁体   English

java - 如何在java-selenium中不使用findElement()或findElements()来检查元素的存在?

[英]How to check the presence of an element without using findElement() or findElements() in java-selenium?

The scenario is to场景是

  1. Find an element in selenium without using findElement() or findElements() method.在不使用 findElement() 或 findElements() 方法的情况下在 selenium 中查找元素。
  2. The code should not throw any exception.代码不应抛出任何异常。 Instead, if the element is found print element found else print element not found.相反,如果找到元素,则找到打印元素,否则找不到打印元素。 (Instead of printing one could do whatever he/she wants to). (而不是打印一个可以做任何他/她想做的事情)。

In order to find the element is present or not, I used JavascriptExecutor in selenium-java.为了查找元素是否存在,我在 selenium-java 中使用了 JavascriptExecutor。

Below is the code.下面是代码。 I have demonstrated the solution using document.getElementById(id) one could you use the other methods like document.getElementsByTagName(name) or document.getElementsByClassName(className) etc.,.我已经使用document.getElementById(id)演示了解决方案,您可以使用其他方法,如document.getElementsByTagName(name)document.getElementsByClassName(className)等。 For demonstration I have used http://automationpractice.com/index.php为了演示,我使用了http://automationpractice.com/index.php

        System.setProperty("webdriver.chrome.driver", "chromedriver_path");
        WebDriver driver = new ChromeDriver();

        driver.get("http://automationpractice.com/index.php");
    
        JavascriptExecutor js = (JavascriptExecutor) driver;
        boolean found = (boolean) js.executeScript(
                "var element = document.getElementById(\"search_query_top\"); if(typeof(element) != 'undefined' && element != null){return true ;}else{return false;}");
        if(found)
            System.out.println("Element Found");
        else
            System.out.println("Elemnet not Found");
        
        driver.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium Webdriver(Java)-如何检查findelement中是否存在标签元素 - Selenium Webdriver (Java) - how to check if tag element exists in findelement 如何使用java-selenium webdriver检查zip文件是否成功下载? - How to check whether a zip file is successfully downloaded or not using java-selenium webdriver? findelements中的硒(java)findelements - selenium (java) findelements in findelements 无法在 Java-Selenium 中找到元素错误 - Unable to locate element error in Java-Selenium 不使用Xpath的Selenium中的FindElement - FindElement in Selenium without using Xpath Selenium findElements()/ findElement(按类)返回null - Selenium findElements() / findElement(by class) returns null 使用 Java Selenium 真正验证元素的存在 - Truly verifying the presence of an element using Java Selenium 如何使用java-selenium中的黄瓜bdd框架处理bootstrap下拉列表 - how to handle bootstrap dropdown using the cucumber bdd framework in java-selenium 如何使用findElements选中dropdownmenu中的复选框并获取? (爪哇/ seleniumWebDriver) - how to check checkbox in dropdownmenu using findElements and get ? (java/seleniumWebDriver) Java Selenium findelement未确认页面上的元素 - Java Selenium findelement is not acknowledging an element that is on the page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM