Python 3.x 如何使用Selenium和Python循环浏览URL列表

Python 3.x 如何使用Selenium和Python循环浏览URL列表,python-3.x,selenium,loops,selenium-webdriver,webdriver,Python 3.x,Selenium,Loops,Selenium Webdriver,Webdriver,大家好,我想在selenium中每循环一次url,当chrome驱动程序弹出时,必须更改列表中的url这是我的想法代码: from selenium import webdriver List = ['http://facebook.com','http://youtube.com','http://google.com'] while True: driver = webdriver.Chrome() driver.get(List) driver.quit(

大家好,我想在selenium中每循环一次url,当chrome驱动程序弹出时,必须更改列表中的url这是我的想法代码:

from selenium import webdriver
    
List = ['http://facebook.com','http://youtube.com','http://google.com']
while True:
    driver = webdriver.Chrome()
    driver.get(List)
    driver.quit()            
尝试itertools.cycle()在列表中无限循环

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

使用range()来限制无限循环。

要使用以下解决方案在URL列表中循环:

  • 代码块:

    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()
    
  • 控制台输出:

    Facebook – log in or sign up
    YouTube
    Google
    

嘿,老兄,谢谢你帮我解决问题,但当我尝试这样做时,它会在我的chrome驱动程序中不断循环,并不断切换URL,所以我不希望出现这种情况。我希望这样,当驱动程序退出时,URL切换到另一个时,不会在一次这样循环URL。嗨,我已经更新了答案,新的代码段应该可以工作,并且将无限循环一个又一个url