0

I am trying to open a list of URLs in new tabs in a single window, but it instead opens them in new windows. Please help. This is my URL.csv

SN,Title,URL
1,Google,https://google.com
2,youtube,https://youtube.com

import csv

from selenium import webdriver


def csv_url_reader(url_obj):
    reader = csv.DictReader(url_obj, delimiter=',')

    for line in reader:
        url = line["URL"]
        title = line["Title"]
        print(url + title)
        driver = webdriver.Chrome(r'C:\chromedriver_win32\chromedriver')
        driver.get(url)



if __name__ == "__main__":
    with open("URL.csv") as url_obj:
        csv_url_reader(url_obj)
gmds
  • 19,325
  • 4
  • 32
  • 58
  • Using javascript and robot class ...you can run open new tab ... I run this script in java .. but for logic you can refer this code .. https://stackoverflow.com/a/56005236/4513879 – Pradnya Bolli May 27 '19 at 07:06

3 Answers3

1

In the piece of code that you posted you are opening the URL in the driver instance and not a new tab.

Instead, you should try opening new tabs like this:

import csv

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

driver = webdriver.Chrome(r'C:\chromedriver_win32\chromedriver')
def csv_url_reader(url_obj):
    reader = csv.DictReader(url_obj, delimiter=',')


    for line in reader:
        url = line["URL"]
        title = line["Title"]
        print(url + title)
        driver.execute_script("window.open(" + url + ",'_blank');")



if __name__ == "__main__":
    with open("URL.csv") as url_obj:
        csv_url_reader(url_obj)
1dra
  • 31
  • 3
0

When you put driver = webdriver.Chrome(r'C:\chromedriver_win32\chromedriver') inside the loop, you are creating a new webdriver instance for each website.

Try something like this:

import csv
import time

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

def csv_url_reader(url_obj):
    reader = csv.DictReader(url_obj, delimiter=',')
    driver = webdriver.Chrome(r'C:\chromedriver_win32\chromedriver')

    for line in reader:
        url = line["URL"]
        title = line["Title"]
        print(url + title)
        driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') 
        driver.get(url)

if __name__ == "__main__":
    with open("URL.csv") as url_obj:
        csv_url_reader(url_obj)
    time.sleep(9999999)

  • Thanks for ur concern bro.Now what is happening that it opens a browser with google.com and instead of opening a new tab on the same tab its opening youtube.com and after that the browser closes.What i want is that it opens google.com in 1st tab and in 2nd tab it open youtube.com and remains open. – Fun4Life May 26 '19 at 20:42
  • Yep, my mistake! Try this: `driver.execute_script(f'''window.open("{url}","_blank");''')` instead of `driver.find_element_by_tag_name` and `driver.get(url)`. If you need to close the first blank window, try switching between tabs like [this](https://stackoverflow.com/questions/28715942/how-do-i-switch-to-the-active-tab-in-selenium) – Victor Hugo Borges May 26 '19 at 20:54
  • its still not working man .In a flick of seconds the browser is closing – Fun4Life May 26 '19 at 21:03
  • Well, can you please define what you want to do after you open these pages? – Victor Hugo Borges May 27 '19 at 03:20
  • I want to open multiple urls on different tab on a single browser.even if i add more urls in my csv.i want the browser to remain open after loading the links.thanks. – Fun4Life May 27 '19 at 20:18
  • Bro everything is working fine all links are opening but on the same tab,It does not open different tabs.It just load each link on the same tab.Seems this driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') Is not working anyhow.Thanks. – Fun4Life May 27 '19 at 23:05
0

To add a new tab you would use send_keys(Keys.COMMAND + 't') for mac, or send_keys(Keys.CTRL + 't') for windows

JROS
  • 71
  • 1
  • 2
  • 11