1

Hello guys I want to loop the url in selenium every one times when it loop it have to change the url in the list when the chrome driver pop up here is my idea code:

from selenium import webdriver
    
List = ['http://facebook.com','http://youtube.com','http://google.com']
while True:
    driver = webdriver.Chrome()
    driver.get(List)
    driver.quit()            
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Phansivang
  • 11
  • 3

2 Answers2

2

To loop through the list of urls using Selenium you can use the following solution:

  • Code Block:

    from selenium import webdriver
    import time
    
    url_list = ['http://facebook.com','http://youtube.com','http://google.com']
    for url in url_list:
        driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe')
        driver.get(url)
        time.sleep(3)
        print(driver.title)
        driver.quit()
    
  • Console Output:

    Facebook – log in or sign up
    YouTube
    Google
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Is there a way to do this without having to reload the driver every time? I'd imagine that would cause the program to slow significantly if you have a large number of URL's (~1000) – F1rools22 Jul 30 '21 at 22:31
0

Try itertools.cycle() to infinitely loop through the list

from selenium import webdriver
import itertools
List = ['http://facebook.com','http://youtube.com','http://google.com']
for url in itertools.cycle(List):
    driver = webdriver.Chrome()
    driver.get(url)
    driver.quit()

Use range() to limit the infinite loop.

  • Hey, man thanks you for help me out but when I try to do that it keeps looping in my chrome driver and keep switching the URL so I don't want like that I want it like when the driver quit and the URL switch to another one does not keep looping the URL in one time like that. – Phansivang Nov 12 '20 at 08:04
  • Hi, I have updated the answer, the new code segment should work and will loop infinitely one url after the other – Naga Giridhar Nov 13 '20 at 14:11