[英]One session per tab with Selenium Web-driver?
我想为我打开的每个选项卡创建一个新的 session,然后使用 Selenium python 单独控制会话。 这可能吗?
@reynoldsnp:Firefox 有一个官方插件可以做到这一点,但我不确定你是否可以让 selenium 与插件交互。 addons.mozilla.org/en-GB/firefox/addon/multi-account-containers 如果你想办法做到这一点,我很想知道怎么做。
(由于我的声誉评分,我还不能发表评论,因此引用了评论)。
我不知道如何与扩展进行实际交互,但如果您有一组已知的要打开的网站:
尝试这个:
制作一个 firefox 配置文件供您与 selenium 一起使用。 多个配置文件
Windows 8/8.1/10:按键盘上的Win + R 。 类型
firefox --new-instance --ProfileManager
通过在设置向导中选择新配置文件,在该配置文件中打开 Firefox。 在该配置文件中安装扩展。
from selenium import webdriver
profile = webdriver.FirefoxProfile('path/to/your/profile') # on windows found here: %APPDATA%/Mozilla/Firefox/Profiles
driver = webdriver.Firefox(firefox_profile=profile)
首先,不,你不能。 虽然选项卡作为进程运行,但它们附加到最初打开浏览器的 session ID。 这就是协议的工作方式 https://www.w3.org/TR/webdriver/#new-session
但是,它们具有唯一的 ID,您可以使用它来识别它们并在它们之间切换。
driver.window_handles
会给你打开的标签列表。 每个选项卡都是完全隔离的。 您现在可以选择
driver.switch_to_window("any open tab taken from windows handles list")
driver.do_something
driver.switch_to_window("any other tab from windows handles list")
driver.do_something_else_on_other_tab
# or (this option can let you run in parallel)
driver a = ChromeDriver()
driver b = ChromeDriver()
a.do_something
b.do_something
按照建议(我个人自己做的)为您想要的每个选项卡打开新的 session,这样您就可以并行它们并运行得更快,总而言之。
我不确定多个浏览器或多个选项卡之间的性能差异是否显着......它们应该使用几乎相同的资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.