Need some help. I got this python code to load url and take a screen print.
I need to achieve this:
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
url = ["http://google.com", "http://cisco.com"]
for element in url:
driver.get(element)
driver.get_screenshot_as_file("screenshot.png")
sleep(2)
driver.quit()
print("end...")
Store the URLs in a text file and then read line by line. Then take screenshot using file name with the host name of the URL.
I have modified your code and could store the screenshot of each url in separate file. I have used Python 3.6.9.
Directory structure:
.
├── links.txt
├── requirements.txt
└── screenshots_of_links.py
links.txt
:
http://google.com
http://cisco.com
requirements.txt
:
selenium==3.141.0
urllib3==1.25.10
screenshots_of_links.py
:
from selenium import webdriver
from urllib.parse import urlparse
from time import sleep
driver = webdriver.Firefox()
with open("links.txt") as url_file:
for line in url_file.readlines():
url = line.strip()
if url != "":
driver.get(url)
host = urlparse(url).hostname
driver.get_screenshot_as_file("{}.png".format(host))
sleep(2)
driver.quit()
print("end...")
Output:
Modification details:
links.txt
text file.urlparse(url).hostname
returns the hostname of a valid URL.Reference: