[Python & Selenium Chromedriver] I have a txt file that has a list of URLs (sitemap.txt) and I want to use that list to automate a script that goes through each URL to validate whether an element (script tag) is present or not. Is there a better way to get the URL's from the txt file to loop into the automation example: get url from txt file > url is opened through chrome browser > inspect webpage > validate https://j.6sc.co/6si.min.js is present> go to the next line of url   txt file looks like https://zendesk.com https://zendesk.com/service   CODE from selenium import webdriver #-*- coding: utf-8 -*- import re import urlparse def findnth(haystack, needle, n): parts= haystack.split(needle, n+1) if len(parts)<=n+1: return -1 return len(haystack)-len(parts[-1])-len(needle) with open("sitemap.txt") as file: for line in file: substring = "https://essentials.zendesk.com/" if substring in line: start = line.find('h') end = findnth(line, "<", 2) print(line[start:end]) for i in line: driver = webdriver.Chrome() driver.get(i) source = driver.find_element_by_xpath("//script[@src='https://j.6sc.co/6si.min.js']");

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

 

[Python & Selenium Chromedriver] I have a txt file that has a list of URLs (sitemap.txt) and I want to use that list to automate a script that goes through each URL to validate whether an element (script tag) is present or not. Is there a better way to get the URL's from the txt file to loop into the automation

example: get url from txt file > url is opened through chrome browser > inspect webpage > validate https://j.6sc.co/6si.min.js is present> go to the next line of url

 

txt file looks like

<url><loc>https://zendesk.com</loc></url>

<url><loc>https://zendesk.com/service</loc></url>

 

CODE

from selenium import webdriver
#-*- coding: utf-8 -*-
import re
import urlparse

def findnth(haystack, needle, n):
parts= haystack.split(needle, n+1)
if len(parts)<=n+1:
return -1
return len(haystack)-len(parts[-1])-len(needle)

with open("sitemap.txt") as file:
for line in file:
substring = "https://essentials.zendesk.com/"
if substring in line:
start = line.find('h')
end = findnth(line, "<", 2)
print(line[start:end])

for i in line:
driver = webdriver.Chrome()
driver.get(i)
source = driver.find_element_by_xpath("//script[@src='https://j.6sc.co/6si.min.js']");

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY