如何使用selenium-webdriver和python在浏览器的单独选项卡中打开多个网页 [英] How to open multiple webpages in separate tabs within a browser using selenium-webdriver and python

查看:499
本文介绍了如何使用selenium-webdriver和python在浏览器的单独选项卡中打开多个网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Selenium Webdriver在同一浏览器窗口中打开多个本地html.我在Jupyter笔记本中尝试了以下操作:

I want to open mulitple local htmls within same browser window using Selenium Webdriver using Python. I have tried following in Jupyter notebook:

from selenium import webdriver

1page = "file://<path for 1.html>"
2page = "file://<path for 2.html>"
firefox_path = 'C:\geckodriver.exe'

driver = webdriver.Firefox(executable_path= firefox_path)
driver.get(1page)
# For opening 2nd HTML in another Tab
driver.execute_script('''window.open('''+ 2page + ''',"_blank");''')

运行上述代码会导致以下错误:

Running above code lead me to the following error:

JavascriptException: Message: Error: Access to 'file://<path of 2.html>' from script denied

如何减轻此错误?

推荐答案

要在浏览器中的单独 TAB 中打开多个 URL /网页您可以使用以下解决方案:

To open multiple URLs / webpages in seperate TABs within a browser you can use the following solution :

  • 代码块:

  • Code Block :

from selenium import webdriver

first_page = "http://www.google.com"
second_page = "https://www.facebook.com/" 
options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get(first_page)
driver.execute_script("window.open('" + second_page +"');")

  • 浏览器快照:

  • Browser Snapshot :

    这篇关于如何使用selenium-webdriver和python在浏览器的单独选项卡中打开多个网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆