python - 如何使用 selenium-webdriver 和 python 在浏览器的单独选项卡中打开多个网页

标签 python selenium selenium-webdriver tabs webdriver

我想使用 Python 使用 Selenium Webdriver 在同一浏览器窗口中打开多个本地 html。我曾尝试在 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");''')

运行上面的代码导致我出现以下错误:

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

如何减轻这个错误?

最佳答案

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

  • 代码块:

    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 +"');")
    
  • 浏览器快照:

multiple_tabs

关于python - 如何使用 selenium-webdriver 和 python 在浏览器的单独选项卡中打开多个网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50522779/

相关文章:

c# - 设置 SpecFlow 以传入标识符

python - selenium webdriver.Firefox 不为非 sudo 用户启动

python - 在python中使用selenium与弹出框交互

python - 根据前一行的值从数据框中删除行

java - 在 gitlab-ci 中运行 selenium 测试用例时出现 chrome 无法访问错误。似乎 headless chrome 存在一些问题,任何人都可以帮忙解决这个问题

java - 使用 geckoddriver 时出错 : Found argument '--websocket-port' which wasn't expected

java - Chromedriver 新更新后,单击方法在 Salesforce 网页上不起作用。用同样的方法如何处理?

jquery - Selenium 测试中 $ 未定义错误

python - 让事件处理函数创建持久的 PYQT4 对象

python - 在索引数组中获取最小值