首页 > 解决方案 > Python Selenium 新标签 Microsoft Edge

问题描述

所以我想使用我想使用 chrome 的 python selenium 模块自动化 Microsoft Edge,但我不知道为什么每当我在我的电脑上打开 chrome 时,我的电脑会自动关闭,所以我使用 Microsoft Edge。我想出了如何使用 Microsoft Edge 制作驱动程序以及如何使用 Microsoft Edge 打开网站,但我的问题是如何在新选项卡中显示链接:

要理解我的问题,这里有一些代码:

from msedge.selenium_tools import Edge, EdgeOptions


class Driver:
    def __init__(self):
       options = EdgeOptions()
       options.use_chromium = True
       self.driver = Edge(options=options)

    def go_to_link(self, *url):
        for i in url:
            self.driver.get(i)

    def close(self):
        self.driver.close()

    def quit(self):
        self.driver.quit()

links = ["https://www.google.co.in/", "https://www.youtube.com/", "https://stackoverflow.com/"]
Driver = Driver()
Driver.go_to_link(*links)
Driver.quit()

所以我希望每个链接都显示在 Microsoft Edge 的新选项卡中,谁能帮我解决这个问题?

如果有人不能提供帮助,我将不胜感激,但仍然可以。

标签: pythonseleniummicrosoft-edge

解决方案


您可以循环访问所有链接,如下所示:

links = ["https://www.google.co.in/", "https://www.youtube.com/", "https://stackoverflow.com/"]
number_of_tabs = len(links)
count = 1
for link in links :
  driver.get(link)
  windows_before = driver.current_window_handle
  driver.execute_script("window.open('');")
  #WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(number_of_tabs))
  new_window = driver.window_handles[count]
  count = count + 1
  driver.switch_to.window(new_window)
  sleep(3)

推荐阅读