Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[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

[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 > repeat validation

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) 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, \"\",> print(line[start:end])

with open(sitemap.txt) as file:

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']\");

This for loop code might be wrong

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions

Question

What is the most important virtue for leaders? Defend your choice.

Answered: 1 week ago