1

I have generated a list of links using selenium,i need to open each link in a separate tab.I have tried body.send_keys(Keys.CONTROL +"t") and body.send_keys(Keys.COMMAND+"t") but both didn't work(no errors but nothing happens), after searching for answers i found this link Opening new tabs selenium,however they mostly used java script(which works it opens a new tab) to run it which i can not seem to manipulate such as driver.execute_script('''window.open("http://bings.com","_blank");''') however i can not use this in a for loop as follows:

for link in links:
    #driver.execute_script("window.open('https://www.yahoo.com')")
    driver.execute_script("window.open('%s')")%link

Edit 1: To possible duplicate,the answers given that work are java script codes which works,however i can't use directly in a for loop.

Do i have to open a new random site (using the java script above) then driver.get(link) to get to my original link

If it matters i use python 2.7 on Linux.

Amjasd Masdhash
  • 178
  • 2
  • 9
  • Possible duplicate of [Selenium multiple tabs at once](https://stackoverflow.com/questions/18150593/selenium-multiple-tabs-at-once) – ivan_pozdeev Feb 10 '19 at 23:20
  • Possible duplicate of https://stackoverflow.com/questions/54330431/open-href-variable-in-a-new-tab/54331766#54331766 – KunduK Feb 10 '19 at 23:53
  • @ivan_pozdeev please check question again,i clearly state that i know its asked before *however* the answers don't complete the answer i asked,the answers on that "Possible duplicate" simply forces me to open a tab(to a random site) then go to the original link . my question was to open new tab with original link.(Please check that link and my exact question before flagging) – Amjasd Masdhash Feb 12 '19 at 19:41

1 Answers1

1

You can create a control_string that you pass as your script:

links = ['https://www.yahoo.com', 'http://bings.com']

for link in links:
    control_string = "window.open('{0}')".format(link)
    driver.execute_script(control_string)
RoyM
  • 735
  • 3
  • 14