Open multiple tabs in selenium using python

こ雲淡風輕ζ 提交于 2021-02-10 20:20:08

问题


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.


回答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)


来源:https://stackoverflow.com/questions/54621313/open-multiple-tabs-in-selenium-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!