虽然元素显示在网页上,但命令.isDisplayed()返回false

时间:2017-05-22 15:19:38

标签: selenium selenium-webdriver selenium-chromedriver

虽然网页上显示图标,标题和子标题,但.isDisplayed()方法始终返回false。

有人可以告诉我为什么它会在网页上为这些元素返回false?

方法:

    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class TeamURLPageFactory {

        static WebDriver driver;
        public static WebElement element = null;
        private static final Logger log = LogManager.getLogger(TeamURLPageFactory.class.getName());
        static String TeamName2 = "BlockedExtContact";
        static String TeamDomain2 = "farzanshaikh.com";

        public static WebElement errorIcon(WebDriver driver){
        element = driver.findElement(By.xpath("//span[@class='error-icon']"));
        log.info("Error Icon Element Found");
        return element;
    }

    public static void errorIconDisplayed(WebDriver driver){
        element = errorIcon(driver);
        if(element.isDisplayed()){
            log.info("Error Icon is Displayed");
        }else{
            log.error("Error Icon is Not Displayed");
        }
    }

    public static WebElement errorHeading(WebDriver driver){
        element = driver.findElement(By.xpath("//div[@id='block-error']/h2"));
        log.info("Error Heading Element Found");
        return element;
    }

    public static void errorHeadingDisplayed(WebDriver driver){
        element = errorHeading(driver);
        if(element.isDisplayed()){
            log.info("Header on the Error Block is displayed");
        }else{
            log.error("Header on the Error Block is not displayed");
        }
    }


    public static WebElement errorSubHeading(WebDriver driver){
        element = driver.findElement(By.xpath("//div[@id='block-error']/p"));
        log.info("Error SubHeading Element Found");
        return element;
    }

    public static void errorSubHeadingDisplayed(WebDriver driver){
        element = errorSubHeading(driver);
        if(element.isDisplayed()){
            log.info("SubHeader on the Error Block is displayed");
        }else{
            log.error("SubHeader on the Error Block is not displayed");
        }
    }

}

主要代码:

import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;;

public class FlockTeamURLDomain {
    static WebDriver driver;
    static String TeamUrl3 = "https://blockedextcontact.flock.com/";
    String TeamName = "<script>farzan</script>";
    String TeamDomain = "farzanshaikh.com";
    private static final Logger log = LogManager.getLogger(FlockTeamURLDomain.class.getName());

    @BeforeClass
    public void beforeClass() {
        System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();

        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(TeamUrl3);
    }
    @Test
    public void blockError() throws Exception{
        driver.get(TeamUrl3);
        Thread.sleep(3000);
        TeamURLPageFactory.errorIconDisplayed(driver);
        TeamURLPageFactory.errorHeadingDisplayed(driver);
        TeamURLPageFactory.errorSubHeadingDisplayed(driver);

    }

    @AfterClass
    public void afterClass() throws Exception {
        Thread.sleep(3000);
        driver.quit();
    }

}

1 个答案:

答案 0 :(得分:2)

isDisplayed()方法的Selenium代码,到这里 - link

 /**
 * Determines whether an element is what a user would call "shown". This means
 * that the element is shown in the viewport of the browser, and only has height and width greater than 0px, and that its visibility is not "hidden".
 * and its display property is not "none".
 * Options and Optgroup elements are treated as special cases: they are
 * considered shown iff they have a enclosing select element that is shown.
 *
 * Elements in Shadow DOMs with younger shadow roots are not visible, and
 * elements distributed into shadow DOMs check the visibility of the
 * ancestors in the Composed DOM, rather than their ancestors in the logical
* DOM.
*

请检查DOM是否隐藏了元素,这可能就是原因。

此外,如果元素显示在网页上,您可以检查element.size()!=0