Selenium Java API provides getTitle method which can be used to get the title of the page currently open in the website.
Below example shows how we can get the title of the web page in java using selenium webdriver.
You may also like to read below topics.
Below example shows how we can get the title of the web page in java using selenium webdriver.
//import the required packages and classes. import java.io.File; import java.util.NoSuchElementException; import java.util.Set; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Keys; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; @SuppressWarnings("unused") public class OpenGoogle { public static void main(String [] arg) { //set the path of the chrome driver exe file System.setProperty("webdriver.chrome.driver", "C:\\Selenuim\\chromedriver2.8.exe"); //create the new instance of the chrome browser WebDriver driver = new ChromeDriver(); try{ driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS); //navigate to given url driver.get("https://www.mindtree.com"); //get the title of the home page at mindetree.com String title = driver.getTitle(); Thread.sleep(2000); } catch(Exception e){ System.out.println("Exception - > " + e.toString()); } finally{ driver.close(); driver.quit(); } } //main function ends }//class endsPlease note that same code works for other browsers like IE, Chrome, SAFARI etc
You may also like to read below topics.
- Press key in Selenium Webdriver in Java
- Read Table Data in Selenium Webdriver in Java
- Take the screenshot in Selenium Webdriver in Java
- Execute JavaScript in Selenium Webdriver in Java
- Handle multiple browser windows in Selenium Webdriver in Java
- Double-Click on the Element in Selenium Webdriver in Java
- Exceptions in Selenium Webdriver in Java
- Synchronization in Selenium Webdriver in Java
No comments:
Post a Comment