0

How can I open a new window and close the previous one, then open again another window and close the previous one. The number of links is indeterminate.

urls = ["https://www.oxxo.com/",
        "https://pypi.org/project/fake-useragent/",
        "https://www.youtube.com/"]

for posts in range(len(urls)):
    print(posts)
    driver.get(urls[posts]) 
    driver.implicitly_wait(val) 
    timestamp = datetime.datetime.now().strftime('%d_%m_%Y')
    chars = [':','/','%'] 
    image_name = driver.current_url
    for char in chars:
        image_name = image_name.replace(char,'_')
    driver.save_screenshot(str(cont)+'_'+image_name+'_'+timestamp+'.png')
    cont += 1   
    if(posts!=len(urls)-1):
       driver.execute_script("window.open('');")
       chwd = driver.window_handles
       driver.switch_to.window(chwd[-1])
driver.close()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Karenmtz
  • 29
  • 2

1 Answers1

0

The urls within the list urls are independent. So neither you need a reference nor you need to open them in the adjacent tab. You can easily invoke them once by one and do your task as follows:

driver = webdriver.Chrome(service=s, options=options)
urls = ["https://www.oxxo.com/",
    "https://pypi.org/project/fake-useragent/",
    "https://www.youtube.com/"]
for url in urls:
    driver.get(url)
    time.sleep(3) # for demonstaration
    print("Do whatever you wish")
driver.quit()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352