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.
-
Possible duplicate of Selenium multiple tabs at onceivan_pozdeev– ivan_pozdeev2019年02月10日 23:20:12 +00:00Commented Feb 10, 2019 at 23:20
-
Possible duplicate of stackoverflow.com/questions/54330431/…KunduK– KunduK2019年02月10日 23:53:01 +00:00Commented 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)Amjasd Masdhash– Amjasd Masdhash2019年02月12日 19:41:11 +00:00Commented Feb 12, 2019 at 19:41
1 Answer 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)