0

I have a text file that generates bunch of urls in text file every hour and the number of urls links varies each time. I want to feed those urls to a python/selenium script one at a time check if those urls can be logged in. This is a testcase that I am creating using python/selenium as part of automated webapp login testcase. . I get the selenium/python part of the script working for single url.

driver.get("https://www.example.com")
print(driver.title)
print(driver.current_url)
userName = driver.find_element_by_name("name")
userName.send_keys("someguy")

How would i feed driver.get() one by one from a text file that has all the Urls.

bern212
  • 23
  • 6
  • in what format are the urls saved in the text file? can you show a part of the text file – Ibrahim Oct 09 '21 at 20:53
  • The text file does not have anything other than the urls as follows. Each line contain single url https://www.webapp_1.com/ https://www.webapp_2.com/ https://www.webapp_3.com/ https://login.appforce1.com/ – bern212 Oct 09 '21 at 21:17
  • Does this answer your question? [How to read a large file - line by line?](https://stackoverflow.com/questions/8009882/how-to-read-a-large-file-line-by-line) – wjandrea Oct 09 '21 at 21:21
  • You'll also need this: [How can I remove a trailing newline?](/q/275018/4518341) – wjandrea Oct 09 '21 at 21:22
  • (Just to be clear, the "large" part of the question I recommended isn't really relevant. This is just the best question I found that covers best practice for opening and reading files.) – wjandrea Oct 09 '21 at 21:25

1 Answers1

2

Heres the basic way


with open("file.txt", "r") as f:
   un_parsed_urls = f.readlines()

parsed_urls = [url.replace("\n", "") for url in un_parsed_urls]


for url in parsed_urls:
   driver.get(url)
   print(driver.title)
   print(driver.current_url)
   userName = driver.find_element_by_name("name")
   userName.send_keys("someguy")

you can use different threads to speed this up

from threading import Thread

def web_automation(url):
   driver.get(url)
   print(driver.title)
   print(driver.current_url)
   userName = driver.find_element_by_name("name")
   userName.send_keys("someguy")

with open("file.txt", "r") as f:
   un_parsed_urls = f.readlines()

parsed_urls = [url.replace("\n", "") for url in un_parsed_urls]

thread_list = []

for url in parsed_urls:
   t = Thread(target=web_automation, args=[url])
   thread_list.append(t)

for t in thread_list:
   t.start()

for t in thread_list:
   t.join()
Ibrahim
  • 798
  • 6
  • 26