I am new bee of the automation testing. On my code, I am using the JavaScript on my Selenium automation test script (on using the JUnit). I am trying to find the current window title:
driver = new ChromeDriver();
js = (JavaScriptExecutor)driver;
driver.get(//passed url);
//after the log in my Org. I execute the command below
//to get the title of the current window I execute the below command
js.executeScript("document.getElementsByTagName('title').innerHTML;");
But the above command will return the answer as the null instant of getting the title of the window. So does anyone know what's wrong with my command?
But I get the title of the window on executing on the console log of the page. So I don't know what's wrong with my code.
Thanks.
Mohan Raj S.
Change the following line:
document.getElementsByTagName('title').innerHTML
to:
return document.getElementsByTagName('title')[0].innerHTML
in code it will be:
js.executeScript("return document.getElementsByTagName('title')[0].innerHTML;");
getElementsByTagName returns array of elements. So we need to pass the index value.
Web driver interface provides get title method. It is very simple.
String title=driver.getTitle();
System.out.println("Title is" + title);