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()            

2 Answers 2

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
    
Sign up to request clarification or add additional context in comments.

1 Comment

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)
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.

2 Comments

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.
Hi, I have updated the answer, the new code segment should work and will loop infinitely one url after the other

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.