-1

I have a bunch of links in a list and I want to open each link in a different tab (only one window). I know how to open a new tab in Selenium but for some reason, when I iterate over the list, all links get open in the same tab and I don't know what I am missing. Could anyone explain me what the error is and how I can fix it? I would really appreciate it.

from selenium import webdriver as wd
from selenium.webdriver.common.keys import Keys

url_list = ["https://www.kdnuggets.com/2017/06/text-clustering-unstructured-data.html", "https://github.com/vivekkalyanarangan30/Text-Clustering-API/", "https://machinelearningblogs.com/2017/01/26/text-clustering-get-quick-insights-from-unstructured-data/", "https://machinelearningblogs.com/2017/06/23/text-clustering-get-quick-insights-unstructured-data-2/", "https://machinelearningblogs.com/2017/06/23/text-clustering-get-quick-insights-unstructured-data-2/"]


driver = wd.Firefox(executable_path="/usr/local/bin/geckodriver")


for url in url_list:
    body = driver.find_element_by_tag_name("body")
    body.send_keys(Keys.COMMAND + "t")
    driver.get(url)

Currently using python3.7, Firefox 65.0.1 and Selenium 3.141 on a Mac

  • 1
    If your `body.send_keys` call opens a new tab then perhaps you should switch to that newly opened "window" before calling `driver.get`. See [this discussion](https://stackoverflow.com/questions/10629815/handle-multiple-windows-in-python) on switching windows. –  Mar 14 '19 at 08:55
  • I did, but that did not resolve the problem. `driver.switch_to_window(driver.window_handles[-1])` didn't work –  Mar 14 '19 at 09:13
  • @CarolinaCárdenas Did new tab open? – Fenio Mar 14 '19 at 09:21

2 Answers2

2

When you open a new tab it is a new window for webdriver which will have its unique handle. driver.window_handles holds the list of active windows, you can use this to switch to newly created window and perform tasks on it.

for url in url_list:
    body = driver.find_element_by_tag_name("body")
    body.send_keys(Keys.COMMAND + "t")
    driver.switch_to_window(driver.window_handles[-1])
    driver.get(url)

Note that you will be using the same variable driver to refer to the newly switched window, so if you close that window then you need to switch to an active window again to perform further tasks.

UPDATE:

If new tab is not opening with your code then you can also try this.

for url in url_list:
    driver.execute_script("window.open()")
    driver.switch_to_window(driver.window_handles[-1])
    driver.get(url)
Kamal
  • 2,384
  • 1
  • 13
  • 25
  • This didn't work. All the links keep opening in the same tab. –  Mar 14 '19 at 09:11
  • 2
    Are new tabs opened? If not then check the updated answer. – Kamal Mar 14 '19 at 09:30
  • send_keys Ctrl+T did NOT open a new tab on my machine (Chrome) so I tried `driver.execute_script("window.open()")` as @Kamal suggested and which does work and `driver.switch_to_window(driver.window_handles[-1])` does switch to the new tab and `driver.get("https://google.com")` does open google in the new tab. I follow the same steps for yahoo.com and again this opens in the new tab. –  Mar 14 '19 at 09:51
  • Hey @Kamal, the `driver.execute_script("window.open()")` did the trick, thank you very much! FYI it seems that `driver.switch_to_window` will get discontinuated, and the new one should be `driver.switch_to.window`. Thanks again! –  Mar 14 '19 at 10:27
0

use window switching with commands

one=driver.window_handles[0] - set the name of the first window

two=driver.window_handles[1] - the name of the second window (after opening it)

driver.switch_to.window(two) - switch to the desired window

  • Isn't this similar to Kamal's answer?: `driver.switch_to_window(driver.window_handles[-1])` –  Mar 14 '19 at 09:15