Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
386 views
by The go-to Tester (324 points)

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

Can we store the page source in selenium?  Is it possible?      
      
if yes, then please tell me, how we can do this?     

Please share the code with me.

      
Thank You.

by The go-to Tester (181 points)
When you said store, does that mean storing source code into some variable in Java? Like string variable?
by The go-to Tester (324 points)
I want to store source code as a string.

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)
selected by
 
Best answer

I do not have your exact HTML structure, so you can first get your HTML page source using,

String HTMLSource  = driver.getPageSource();

Above code will return complete HTML source of the page.

Once Page Source is stored as a string, you can do string menupulation.

Eg.

String HTMLSource = driver.getPageSource();
int indexOfTitle = HTMLSource.indexOf("title");
String subStringFromTitle = HTMLSource.substring(indexOfTitle);
int indexOfMeta = subStringFromTitle.indexOf("meta");
String text = HTMLSource.substring(indexOfTitle , indexOfTitle + indexOfMeta);
 
Hope that helps!


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...