Search code examples
seleniumselenium-webdriverjunit4browser-automation

printing of header in Indiatimes website fails


I am trying to print the header "The Times of India" in the website "https://timesofindia.indiatimes.com/us" but it fails..I think the problem is the popup notification which asks to give an input... I tried alert but that doesn't help... can someone help me in fix the code below..

public static void main(String[] args) {
        // TODO Auto-generated method stub
    WebDriver driver = utils.HelperFunctions2.createAppropriateDriver("Chrome");
        
        driver.get("https://timesofindia.indiatimes.com/");
        System.out.println("****Main page loaded*****");
        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        alert.accept();
        String header = driver.findElement(By.xpath("//*[@id=\"header-masthead\"]/div/a/img")).getText();
        System.out.println(header);
    }

Solution

  • This is because "The Times of India" is a Picture, not a Plain text! Webdriver can not interpret image as a text.

    What you can do in this case is to check attribute "src":

    String titlePicture = driver.findElement(By.xpath("//*[@id=\"header-masthead\"]/div/a/img")).getAttribute("src");
    

    It should be equal to https://static.toiimg.com/photo/77144633.cms - source of "The Times of India" picture