Webdriver — Java How to capture meta tags in a web page and verify it’s content ?
As a part of a testing requirement , I want to verify the meta tags of below HTML , how can I archive this ?
<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="description" content="DemoMetaTagDescription"> <!-- Define a description of your web page: --><meta name="keywords" content="HTML, CSS, XML, JavaScript"> <!--Define keywords for search engines: --><meta name="author" content="Sam Winchester"> <!--Author --><meta http-equiv="refresh" content="30"> <!-- Refresh the page in every 30 seconds --><meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- The initial-scale=1.0 part sets the initial zoom level when the page is first loaded by the browser. The viewport is the user's visible area of a web page It varies with the device, and will be smaller on a mobile phone than on a computer screen. --><base href="https://www.w3schools.com/" target="_blank"> <!--Specify a default URL and a default target for all links on a page:There can be at maximum one <base> element in a document, and it must be inside the <head> element.https://www.w3schools.com/tags/tag_base.asp --><title>Learn HTML</title><style>body {background-color: powderblue;}h1 {color: red;}p {color: blue;}</style></head><body><h1>This is a Heading</h1><p>This is a paragraph.</p></body></html>
Answer- You can capture the meta tags like a normal element using it’s attribute and verify the value .
Example in a normal button xpath is as per below .
//button[@id=’upload’]
Element type is Button , attribute is id and value is upload
The similar thing can be used fo e meta tags .let’s check below tag.
<meta name=”description” content=”DemoMetaTagDescription”>
Element type is meta, attribute is name and value is description.So the final element is
WebElement descEle=driver.findElement(By.xpath(“//meta[@name=’description’]”));
Full code available here .
package com.webdriver.blogs;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;/*
*/public class WebdriverMetaTag {
static WebDriver driver;
@Test
public void StartBrowser() throws InterruptedException{
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\Jar_files\\chromedriver.exe");
driver= new ChromeDriver();
driver.manage().timeouts().implicitlyWait(29, TimeUnit.SECONDS);
//wait until 60 seconds URL to load,unless go to timeout
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
//Maximize window
driver.manage().window().maximize();
//Delete all cookies
driver.manage().deleteAllCookies();
}
@Test(priority=1,enabled=true,description="Meta tag verification")
public void checkMetaTag() {
driver.get("file:///C:/Users/sdesilva2/eclipse-workspace/sam-webdbriver-diaries/src/test/java/com/webdriver/blogs/index.html");
WebElement descEle=driver.findElement(By.xpath("//meta[@name='description']"));
WebElement authorEle=driver.findElement(By.xpath("//meta[@name='author']"));
WebElement refreshEle=driver.findElement(By.xpath("//meta[@http-equiv='refresh']"));System.out.println("Meta author attribute is "+authorEle.getAttribute("content"));
System.out.println("Meta description attribute is "+descEle.getAttribute("content"));
System.out.println("Meta refresh attribute is "+refreshEle.getAttribute("content"));WebElement charuthorEle=driver.findElement(By.xpath("//meta[@charset='UTF-8']"));
System.out.println(" Element found "+charuthorEle.toString());WebElement baseHrefrEle=driver.findElement(By.xpath("//base[@href='https://www.w3schools.com/']"));
System.out.println("baseHrefrEle attribute is "+baseHrefrEle.getAttribute("target"));}
@AfterClass()
public void shutDown() {
driver.close();
driver.quit();
}}
Output is
Meta author attribute is Sam WinchesterMeta description attribute is DemoMetaTagDescriptionMeta refresh attribute is 30Element found [[ChromeDriver: chrome on XP (05ea58a0b2dfcc67056aeacbd6ed68a7)] -> xpath: //meta[@charset='UTF-8']]baseHrefrEle attribute is _blank
Video tutorial- https://youtu.be/LTXyv1hJYv4