Chapter-4 Update
Chapter-4 Update
(OR)
Q2. Write a python program to use the get method of request library to get website url
import requests
url = 'https://www.utas.edu.om'
response = requests.get(url)
print('URL:',response.url)
Output:
Q3. Write a python program to use the get method of request library to get status of web
request
200-Successful (‘https://www.scht.edu.om’)
403-Forbidden (‘https://portal.scht.edu.om’)
404-Website Not found (‘https://portal.schte.edu.om’)
import requests
url = 'https://www.shct.edu.om'
response = requests.get(url)
print('STATUS CODE:',response.status_code)
print('STATUS REASON:',response.reason)
Output:
Q4. Write a python program to use the get method of request library to get status of web
request
import requests
url = 'https://portal.shct.edu.om'
response = requests.get(url)
print('STATUS CODE:',response.status_code)
print('STATUS REASON:',response.reason)
Output:
Q5. Write a python program to use the get method of request library to get status of
web request
import requests
url = 'https://portal.shct.edu.om/page'
response = requests.get(url)
print('STATUS CODE:',response.status_code)
print('STATUS REASON:',response.reason)
Output:
Q6. Write a python program to use the get method of request library to get website
headers
import requests
url = 'https://www.shct.edu.om'
response = requests.get(url)
print('HTTP Header :',response.headers)
Output:
Q7. Write a python program to use the get method of request library to download a file.
(winrar.exe)
import requests
resp=requests.get("https://www.win-rar.com/fileadmin/winrar-versions/winrar/
winrar-x64-624.exe")
file=open("winrar2.exe","wb")
file.write(resp.content)
file.close()
Q8. Write a python program to use the get method of request library to
download a file. (winrar.exe)
import requests
resp=requests.get('https://hips.hearstapps.com/hmg-prod/images/new-york-skyline-
on-a-sunny-day-with-clear-blue-sky-royalty-free-image-1577127184.jpg')
img=resp.content
file=open('building.jpg','wb')
file.write(img)
Q9. Write a python program to use the get method of request library to login to a
website (username and password)
Q1. Write a python program using urllib to display the html contents of a website.
import urllib.request
resp=urllib.request.urlopen('https://www.shct.edu.om')
print(resp.read().decode('utf-8'))
Q2. Write a python program to use post method to test a user login online.
#https://httpbin.org/basic-auth/user/pass
import requests
payload={'Username':'user','Password':'pass'}
resp = requests.post('https://httpbin.org/post', data=payload)
print(resp.text)
Q3. Write a python program to get the number of cookies used by the website.
from urllib.request import urlopen, build_opener, HTTPCookieProcessor
from http.cookiejar import CookieJar
cookie_jar = CookieJar()
opener = build_opener(HTTPCookieProcessor(cookie_jar))
opener.open("https://www.google.com")
print(len(cookie_jar))
Webbrowser: Convenient web-browser controller
https://docs.python.org/3/library/
https://docs.python.org/3/library/webbrowser.html Documentation
Q1. Write a python program using webbrowser to display the website in a browser.
import webbrowser
webbrowser.open('https://www.shct.edu.om')
Q2. Write a python program using webbrowser to search for the keyword in google
website.
import webbrowser
searchkey=input('enter string to search in google')
webbrowser.open('https://www.google.co.in/search?q=' + searchkey)
Q3. Write a python program using webbrowser to open the default in staffportal website.
import webbrowser
webbrowser.open('https://portal.shct.edu.om/staffportal/shctmainNew')
Q4. Write a python program using webbrowser to open the default in google website.
webbrowser.open(url): Display url using the default browser. url is opened in the same
browser window if possible.
Selenium web driver is a web based automation testing framework which can test
webpages initiated on various web browsers like chrome, firefox, microsoftedge and
operating systems like windows, linux, android, apple and you can also write test scripts
on different languages python, java, php, c#
Selenium is an open source tool which is used for automating the test carried out on
different web browsers
It is easy to use and it is simple
It has a wide variety of languages support to automate the process.
Q5. Write a python program using selenium to login automatically to a local web
page inside the UTAS shinas campus(192.168.106.9:81)
passwd=driver.find_element('xpath','/html/body/form/div[2]/input[2]')
passwd.send_keys('12345678')
time.sleep(5)
log_btn=driver.find_element('xpath','/html/body/form/div[2]/input[3]')
log_btn.click()
time.sleep(25)
Q6. Write a python program using selenium to login automatically to a local web
page inside the UTAS shinas campus(192.168.106.9:81)
menu_btn=driver.find_element('xpath','/html/body/form/fieldset[1]/input[1]')
menu_btn.send_keys('Mohammed Naheed')
time.sleep(5)
dob=driver.find_element('xpath','/html/body/form/fieldset[1]/input[2]')
dob.send_keys('11/11/2021')
time.sleep(5)
emai=driver.find_element('xpath','/html/body/form/fieldset[1]/input[3]')
emai.send_keys('[email protected]')
time.sleep(5)
pass1=driver.find_element('xpath','/html/body/form/fieldset[1]/input[4]')
pass1.send_keys('testing')
time.sleep(5)
feed=driver.find_element('xpath','/html/body/form/fieldset[2]/textarea')
feed.send_keys('Thanks for the feedback support field')
time.sleep(25)
Q7. Write a python program to implement the GET request function on a local webserver.
(python -m http.server to test the webserver)