This process of creation of HTML pages using python is similar to process of creation of any other file.
To automatically open the pages after execution, we will use selenium webdriver.
Following are the steps:
1. Open any text editor and create a python file with .py extension.
2. Import selenium packages. (Make sure you download them first.)
For downloading on Ubuntu, use these commands in terminal:
$ sudo apt-get install python-pip
$ sudo pip install selenium
Import:
from selenium import webdriver
3. Write the following to create HTML file on execution.
f = open('helloworld.html','w')
4. In order to write in this file, use the following code:
5. Close command :
f.close()
To automatically open the pages after execution, we will use selenium webdriver.
Following are the steps:
1. Open any text editor and create a python file with .py extension.
2. Import selenium packages. (Make sure you download them first.)
For downloading on Ubuntu, use these commands in terminal:
$ sudo apt-get install python-pip
$ sudo pip install selenium
Import:
from selenium import webdriver
3. Write the following to create HTML file on execution.
f = open('helloworld.html','w')
f.write("""<!DOCTYPE HTML>
<html> <header><title>This is title</title></header> <body> Hello world </body> </html>
""")
f.close()
6. Use the following snippet to open the page automatically on execution:
Note: I have used Chrome. You can use Firefox if you want.
browser = webdriver.Chrome()
browser.get('file:PATH/helloworld.html')
Replace the PATH with the path of the folder where this program is being written.
That's all!
(Bonus):
Here you can add huge HTML codes. If you wish to add some custom strings from the program, use the following while writing the file:
f.write("""<!DOCTYPE HTML>
<html>
<header><title>This is title</title></header>
<body>
Hello world
<h3>{0}</h3>
<h2>{1}</h2>
</body>
</html>
""".format(str1,str2))
Although this method is simple, this is not the correct way of designing HTML sites using python. Therefore, once created, there is no python interaction with the webpage. You may use other automation tools in python to improve its functionality.
Comments
Post a Comment