本文介绍了使用 python 在 selenium 中打开多个选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我使用 selenium 生成了一个链接列表,我需要在单独的选项卡中打开每个链接.我尝试过 body.send_keys(Keys.CONTROL +"t")body.send_keys(Keys.COMMAND+"t") 但两者都不起作用(没有错误但没有任何反应),在搜索答案后我找到了这个链接 打开新标签硒,但是他们主要使用java脚本(它可以打开一个新标签)来运行它我似乎无法操纵,例如driver.execute_script('''window.open("http://bings.com","_blank");''') 但是我不能在 for 循环中使用它,如下所示:

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 asdriver.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:为了可能重复,给出的答案work 是可以工作的 java 脚本代码,但是我不能直接在 for 循环中使用.

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.

我是否必须打开一个新的随机站点(使用上面的 java 脚本)然后 driver.get(link) 才能访问我的原始链接

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

如果重要的话,我在 Linux 上使用 python 2.7.

If it matters i use python 2.7 on Linux.

推荐答案

您可以创建一个 control_string 作为脚本传递:

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)

这篇关于使用 python 在 selenium 中打开多个选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:16