Getting the source of the Page in Selenium with Python

Selenium has inbuilt code to get the source of the WebPage in Python.All we have to do is use that code and get the source and store it for further manipulation.

a=driver.page_source

Here is an example in which I have used the Python code and got the source of StackOverflow itself

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import gmtime, strftime
import time

#Getting the source of the webpage and storing it as Text
driver = webdriver.Firefox()
driver.maximize_window()
url ="http://stackoverflow.com/questions/tagged/selenium?page=1&sort=newest&pagesize=15"
driver.get(url)
a=driver.page_source
name = 'a1.txt'
try:
    file = open(name,'w',encoding='utf-8')
    file.write(a)
    file.close()

except:
     print('page'+str(x)+'\n')
       
a=""
time.sleep(3)
   
driver.close()

 

You may also like...