9

How can I view the source of a page between the "title" and "meta" tags using Selenium WebDriver with Java?

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
newcane
  • 285
  • 2
  • 5
  • 8

2 Answers2

15

You can try driver.getPageSource() after you have loaded the page.

link to java doc

Stephan
  • 41,764
  • 65
  • 238
  • 329
Sam
  • 2,427
  • 2
  • 23
  • 26
7

You can compare the Title of a page as below code:

String actualTitle = driver.getTitle();
String expectedTitle = "My Title";
assertEquals(actualTitle, expectedTitle);

If you want to get page source you can get by using the following java code:

String pageSource = driver.getPageSource();

If you want to verify a particular text is present or not on the page, do as below:

boolean isTheTextPresent = driver.getPageSource().contains("your text");
assertTrue(isTheTextPresent);
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176