<<Previous Post << Complete Tutorial>> Next Post>>
In the previous article, I have practically demonstrated retrieving the Current Page URL using Selenium C#.
In this article, I am going to practically demonstrated retrieving the Source code of the current page using Selenium C# API command PageSource.
Let’s get started.
Selenium C Sharp – Retrieving the Page Source
In this article, I am going to practically demonstrate by providing the task first followed by solution.
Task: Open the http://omayo.blogspot.com URL in Chrome browser and retrieve the Source code of the Current Page.
If we have to manually retrieve the source code of the above page, we need to right click on the page and select ‘View Page Source’ option as shown below:
The source code of the above page will be displayed in the new tab as shown below:
Solution: Lets follow the below steps:
1) Launch Visual Studio and select an existing project which is already configured with Selenium and WebDriverManager as shown below:
2) Write the below code explained in the previous posts for navigating to the http://omayo.blogspot.com URL in chrome browser in maximized mode as shown below:
3) Now use the PageSource to retrieve the Source code of the page and hover the mouse to see the return type of PageSource command as shown below:
4) Observe that the return type of PageSource is String, hence create a variable for String Class and assign the value retrieved by PageSource to the variable as shown below:
String pageSource = driver.PageSource;
5) Print the above retrieved and stored source code of the page in the variable as shown below:
6) Save and click on ‘Start’ button to execute the above code.
Observe that the Selenium C# Script will get executed and the Source code of Current URL of the page will be retrieved and printed as shown below:
Here concludes this article.
In the next article, I will choose another Selenium command for performing different operation on the web page.
Next Steps:
- > To learn more about Selenium C#, continue to the next post (Click on Next Post Link Below)
- > Check complete Selenium C# Tutorial Contents here (Click here)
Please leave your questions/comments/feedback below.
Happy Learning ?
Connect to me on Linked In (Click here)
On a mission to contribute to the Software Testing Community in all possible ways.
<<Previous Post << Complete Tutorial>> Next Post>>