python - 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 的新选项卡中,谁能帮我解决这个问题?
如果有人不能提供帮助,我将不胜感激,但仍然可以。
解决方案
您可以循环访问所有链接,如下所示:
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)
推荐阅读
- javascript - 将聊天气泡内容向右对齐
- mysql - MySQL 5.7 JSON_EXTRACT 不适用于对象中的带引号的字符串:[错误]“字符串中缺少右引号”
- json - 如何将 JSON 字符串设置为 Postman 变量并在正文中使用它?
- math - 在实时应用中,是否有一种数值方法可以在 t = 0 s 处逼近一阶导数?
- azure-sql-database - 如何将其他活动目录登录添加到 Azure 数据库?
- java - 我不同步读取不可变对象保存?
- datetime - 使用电子表格中的格式在脚本中显示日期和时间信息
- magento2 - 无法扩展/覆盖父“移动”元素
- c++ - 使用单个生成器从多个分布中获取值
- python-3.x - 在不使用 .apply 的情况下对 panda 数据框中的单列执行简单操作