【问题标题】:How to open multiple webpages in separate tabs within a browser using selenium-webdriver and python如何使用 selenium-webdriver 和 python 在浏览器中的单独选项卡中打开多个网页
【发布时间】:2018-05-25 06:26:10
【问题描述】:

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

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

如何缓解这个错误?

【问题讨论】:

    标签: python selenium selenium-webdriver tabs webdriver


    【解决方案1】:

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

    • 代码块:

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

    【讨论】:

    • 感谢您的回复。但是,本地 html 页面呢?
    • get() 方法接受 URL 作为String。所以你可以通过传递local html pages来使用get(),就像我们将first_pagesecond_page作为变量传递给get()一样
    • 除了我评论的内容之外,您是否发现解决方案有任何不同?你到底卡在哪里了?你看到任何错误吗?请用您的代码试用和错误堆栈跟踪更新问题。
    • 我已经尝试了您的建议。它在新窗口而不是当前窗口中打开所述选项卡。它还向我展示了WebDriverException: Message: unknown error: call function result missing 'value' (Session info: chrome=66.0.3359.181) (Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 6.1.7601 SP1 x86_64)
    • @abhi1610 错误call function result missing 'value' 是一个常见错误,源于您使用的二进制文件的不兼容版本,与此问题/答案无关。关注Selenium WebDriverException: unknown error: call function result missing 'value' while calling sendkeys method讨论解决。
    猜你喜欢
    • 2018-08-14
    • 2018-02-01
    • 2015-07-10
    • 1970-01-01
    • 2022-12-28
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多