M1999
M1999

Reputation: 15

Python Selenium loop keeps using the first URL, doesnt quit loop

I need to:

  1. Open a new browser window.
  2. Open a from an array URL
  3. Do some actions (not shown)
  4. Quit the browser window
  5. Loop until all URLs have been opened.

ACTUAL RESULT: It keeps opening a new browser window, and only goes to the first URL (google.com), then closes it... and re-opens a new window at Google.com. The loop is infinite and doesn't stop after 3 URLs in this example.

EXPECTED/DESIRED RESULT: Loop through the URLs, and then stop the script once done (in this case, opening the browser 3 times, loading 3 different URLs, closing each browser)

Any help would be appreciated.

This is the code I tried

urlstoloop = ["http://google.com",
"http://yahoo.com",
"http://msn.com"]

i = 0
while i < len(urlstoloop):
  web = webdriver.Chrome()
  web.get(urlstoloop[i])





  CUSTOM CODE



  web.quit()



i += 1

Upvotes: 0

Views: 179

Answers (2)

Anis R.
Anis R.

Reputation: 6912

You are incremenitng i outside of the loop. This should be done inside the loop, so indent it in:

while i < len(urlstoloop):
    #something...
    i += 1

Also, as suggested by @Alderven, for this case it's better to use a for loop to iterate over the list of URLs. Just wanted to point out the mistake in your existing code.

Upvotes: 0

Alderven
Alderven

Reputation: 8270

Use for instead of while:

for url in urlstoloop:

Upvotes: 2

Related Questions