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.

asked Feb 10, 2019 at 21:36
3
  • Possible duplicate of Selenium multiple tabs at once Commented Feb 10, 2019 at 23:20
  • Possible duplicate of stackoverflow.com/questions/54330431/… Commented Feb 10, 2019 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) Commented Feb 12, 2019 at 19:41

1 Answer 1

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)
answered Feb 10, 2019 at 23:21
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.